Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
241 views
in Technique[技术] by (71.8m points)

python - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...