How to have image + text in one button in Tkinter

后端 未结 1 1236
旧时难觅i
旧时难觅i 2020-12-16 00:15

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 modify

相关标签:
1条回答
  • 2020-12-16 00:36

    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()
    
    0 讨论(0)
提交回复
热议问题