Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have image + text in one button in Tkinter

I´m trying to create a button, which will include an image aligned to the left and text aligned to the right. I just want to change text by parameter "text", not by modifying whole image. Is this possible somehow?

Here´s a simple example, what I mean.

http://img651.imageshack.us/img651/3776/previewrv.png

Hope I explained it well

Thank you

like image 421
Milan Skála Avatar asked Feb 21 '13 17:02

Milan Skála


People also ask

Can we insert an image in a button in Tkinter?

Tkinter has a feature to add images to Tkinter buttons. This is useful for users to remember the images in the GUI rather than the text. In the program below, we have used PhotoImage method of imageKT module to add images to Tkinter buttons, and we don't forget to mention the local path to the image file.

What is compound in Tkinter?

To display both text and image, you'll use the text attribute and compound option. The compound option specifies the position of the image relative to the text.


1 Answers

Look at the compound option to the label. It lets you specify the relationship of the label to the text (top, bottom, left, right, none).

For example:

import Tkinter as tk

class View(tk.Frame):
    def __init__(self, *args, **kwargs):
        tk.Frame.__init__(self, *args, **kwargs)
        self.image = tk.PhotoImage(file="somefile.gif")
        b = tk.Button(self, text="Hello, world", image=self.image, compound="left")
        b.pack(side="top")

if __name__ == "__main__":
    root = tk.Tk()
    view = View(root)
    view.pack(side="top", fill="both", expand=True)
    root.mainloop()
like image 151
Bryan Oakley Avatar answered Sep 23 '22 00:09

Bryan Oakley