繁体   English   中英

使用Tkinter的背景图片-Python 2.7

[英]Background image using Tkinter - Python 2.7

我试图在图像上显示一个按钮。该按钮似乎在图像的前面。 如何将图像设置为应用程序的背景? 该按钮还需要更改其位置,因此图像应该是永久的。

我的代码:

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

答案,我怀疑,是在这里 :“[代码]包在一个框架部件一个部件的标签,然后在画面右上角放置一个按钮,此按钮将重叠的标签。” 我这样测试

from tkinter import *

class App:
    def __init__(self,master):
        master.geometry("400x200")

        photo = PhotoImage(file="C:/programs/Python34/temimage.png")
        self.w = w = Label (master, image=photo)
        w.photo = photo
        w.pack()

        self.hello_b = Button(master,text="ME",command=sys.exit, height=1,
                              width=3,fg= "white",bg = "green")
        self.hello_b.place(x=200, y=5)

root = Tk()
app = App(root)
root.mainloop()

该按钮确实位于图像的顶部。 x,y位置似乎在按钮的左上角。 新增中

def move():
    app.hello_b.place(x=100, y=100)
root.after(1000, move)

导致按钮在一秒钟后跳动,正如预期的那样。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM