Python 如何将按钮放在图像顶部?(特金特)

Python 如何将按钮放在图像顶部?(特金特),python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我怎样才能把按钮放在图像上并保持在窗口的中心?代码工作正常,但按钮在图像下,我无法单击它 from tkinter import Frame, Tk, Label, Button from PIL import Image, ImageTk class Example(Frame): def __init__(self, master, *pargs): Frame.__init__(self, master, *pargs) self.image =

我怎样才能把按钮放在图像上并保持在窗口的中心?代码工作正常,但按钮在图像下,我无法单击它

from tkinter import Frame, Tk, Label, Button
from PIL import Image, ImageTk

class Example(Frame):
    def __init__(self, master, *pargs):
        Frame.__init__(self, master, *pargs)

        self.image = Image.open("folder\\file.gif")
        self.img_copy= self.image.copy()

        self.background_image = ImageTk.PhotoImage(self.image)

        self.background = Label(self, image=self.background_image)
        self.background.pack(fill="both", expand="YES")
        self.background.bind('<Configure>', self._resize_image)

    def _resize_image(self,event):

        new_width = event.width
        new_height = event.height

        self.image = self.img_copy.resize((new_width, new_height))

        self.background_image = ImageTk.PhotoImage(self.image)
        self.background.configure(image =  self.background_image)

root = Tk()
root.title("Title")
root.geometry("600x600")
root.configure(background="black")

e = Example(root)
e.pack(fill="both", expand="YES")

btn=Button(root,text="Hello World").pack()

root.mainloop()
从tkinter导入框架、Tk、标签、按钮
从PIL导入图像,ImageTk
类示例(框架):
定义初始值(自、主、参数):
帧。初始帧(自、主、*pargs)
self.image=image.open(“文件夹\\file.gif”)
self.img_copy=self.image.copy()
self.background\u image=ImageTk.PhotoImage(self.image)
self.background=标签(self,image=self.background\u image)
self.background.pack(fill=“both”,expand=“YES”)
self.background.bind(“”,self.\u调整大小\u图像)
def_resize_图像(自身、事件):
新建宽度=event.width
新高度=事件高度
self.image=self.img\u copy.resize((新宽度、新高度))
self.background\u image=ImageTk.PhotoImage(self.image)
self.background.configure(image=self.background\u image)
root=Tk()
根标题(“标题”)
根部几何形状(“600x600”)
root.configure(background=“black”)
e=示例(根)
e、 打包(fill=“两者”,expand=“是”)
btn=按钮(root,text=“Hello World”).pack()
root.mainloop()

我不知道这是否是您真正应用程序的正确解决方案,但对于问题中的代码,最简单的解决方案是使用
place
。使用
place
可以使用相对位置将一个小部件保持在另一个小部件的中心位置

place
也不是通用布局管理器的最佳选择,但对于非常特定的用例,它是适合该工作的工具。在下面的示例中,通过使用
relx
rely
锚定将按钮放置在
示例
框架的中心。参数中的
指定相对坐标相对于哪个小部件

btn=Button(root,text="Hello World")
btn.place(in_=e, relx=.5, rely=.5, anchor="c")

您不能使用
.pack()
重叠窗口小部件。您可以使用
.grid()
(将它们放在同一行/列中)或
.place()