如何在Tkinter中将图片和文字放在一个按钮上
我想创建一个按钮,这个按钮左边有一张图片,右边有一些文字。我只想通过一个叫“text”的参数来改变文字,而不是每次都去修改整个图片。这样做有可能吗?
这里有一个简单的例子,说明我想要的效果。
http://img651.imageshack.us/img651/3776/previewrv.png
希望我解释得够清楚
谢谢
1 个回答
31
看看标签的 compound
选项。这个选项可以让你指定标签和文本之间的关系,比如在上面、下面、左边、右边或者不显示。
举个例子:
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()