【发布时间】:2015-08-30 13:42:07
【问题描述】:
我正在尝试在图像上显示一个按钮。该按钮似乎位于图像的前面。 如何将图像设置为应用程序的背景? 按钮也需要改变他的位置,所以图像应该是永久的。
我的代码:
from Tkinter import *
class App:
def __init__(self,master):
frame = Frame(master)
master.geometry("400x200")
frame.pack()
self.hello_b = Button(master,text="ME",command=sys.exit, height=1, width=3,fg= "blue",bg = "green")
self.hello_b.pack()
photo = PhotoImage(file="unnamed.gif")
w = Label (master, image=photo)
w.place(x=0, y=0, relwidth=1, relheight=1)
w.photo = photo
w.pack()
root = Tk()
app = App(root)
root.mainloop()
【问题讨论】:
-
有几处错误。有很多代码与您的问题无关,这意味着您在测试之前编写了太多代码。写并发布一个 mcve stackoverflow.com/help/mcve。您创建并打包了一个未使用的框架。您在打包的 Button 顶部放置了一个带有照片的标签。然后您再次打包照片。
-
我编辑并清理了一些东西。我希望现在它更清楚了。
-
我在 3.5 中对 .png 的实验表明需要设置 w.photo 属性,但这非常不寻常,并且不在我拥有的 tkinter 文档中。你怎么知道这样做的?
-
通过互联网.. 我对 python 很陌生,并试图从我能阅读的内容中收集知识。我有想要展示的 .gif 文件并使用了它。您能否建议一种在背景中显示 .gif 的方法,而按钮位于其上方?
标签: python image python-2.7 button tkinter