Tkinter:在背景图像上叠加标签
Tkinter: Overlaying a label on top of a background image
我正在尝试使用 Python 和 Tkinter 创建我的第一个 GUI。我想要一个根据 window 大小相应调整大小的背景图像,以及背景顶部的两个标签,它们都位于 window 的中间位置。这两个标签是 "Full Name" 和 "Education",如下面我的代码所示。
目前,我正在使用 pack() 方法,并且一直在使用 here 中的 window 调整大小代码。
我的问题是:如何让标签与背景图像(也是我的代码中的标签)重叠?使用我当前的代码,背景图像似乎位于框架和标签之上。
附件是我正在寻找的 output/GUI 的图片,但我希望我的图片位于背景中。
GUI
#Resize using label
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
root.geometry('600x600')
def resize_image(event):
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image = photo)
label.image = photo #avoid garbage collection
#Background image
image = Image.open("filepath.jpg")
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = Label(root, image = photo)
label.bind('<Configure>', resize_image)
label.place(x=0, y=0, relwidth=1, relheight=1)
label.pack(fill=BOTH, expand = YES)
label.lower()
frame = Frame(root, width=600, height=600, relief='raised', borderwidth=2)
frame.pack(fill="both", expand=True)
frame.pack_propagate(False)
#Top Frame
top_frame = Frame(frame,width=600, height=350)
top_frame.pack(side = TOP)
#Various Labels
Label(frame, text = 'Full Name', width = 8).pack()
Label(frame, text = 'Education', width = 8).pack()
root.mainloop()
一些重新排列是有序的。您的大 frame
位于背景图片之上,完全覆盖了它。因此,让我们将背景 Label
设为 frame
的一部分,而不是 root
。您可能应该选择 place()
或 pack()
,但不能同时选择两者。为了让其他标签居中,我创建了一个居中框架并将它们打包到其中。可能还有其他方法可以完成所有这些:
from tkinter import *
from PIL import Image, ImageTk
def resize_image(event):
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image=photo)
label.image = photo # avoid garbage collection
root = Tk()
root.title("Title")
root.geometry('600x600')
frame = Frame(root, relief='raised', borderwidth=2)
frame.pack(fill=BOTH, expand=YES)
frame.pack_propagate(False)
copy_of_image = Image.open("filepath.jpg")
photo = ImageTk.PhotoImage(copy_of_image)
label = Label(frame, image=photo)
label.place(x=0, y=0, relwidth=1, relheight=1)
label.bind('<Configure>', resize_image)
center_frame = Frame(frame, relief='raised', borderwidth=2)
center_frame.place(relx=0.5, rely=0.5, anchor=CENTER)
Label(center_frame, text='Full Name', width=8).pack()
Label(center_frame, text='Education', width=8).pack()
root.mainloop()
我正在尝试使用 Python 和 Tkinter 创建我的第一个 GUI。我想要一个根据 window 大小相应调整大小的背景图像,以及背景顶部的两个标签,它们都位于 window 的中间位置。这两个标签是 "Full Name" 和 "Education",如下面我的代码所示。
目前,我正在使用 pack() 方法,并且一直在使用 here 中的 window 调整大小代码。
我的问题是:如何让标签与背景图像(也是我的代码中的标签)重叠?使用我当前的代码,背景图像似乎位于框架和标签之上。
附件是我正在寻找的 output/GUI 的图片,但我希望我的图片位于背景中。
GUI
#Resize using label
from tkinter import *
from tkinter import ttk
from PIL import Image, ImageTk
root = Tk()
root.title("Title")
root.geometry('600x600')
def resize_image(event):
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image = photo)
label.image = photo #avoid garbage collection
#Background image
image = Image.open("filepath.jpg")
copy_of_image = image.copy()
photo = ImageTk.PhotoImage(image)
label = Label(root, image = photo)
label.bind('<Configure>', resize_image)
label.place(x=0, y=0, relwidth=1, relheight=1)
label.pack(fill=BOTH, expand = YES)
label.lower()
frame = Frame(root, width=600, height=600, relief='raised', borderwidth=2)
frame.pack(fill="both", expand=True)
frame.pack_propagate(False)
#Top Frame
top_frame = Frame(frame,width=600, height=350)
top_frame.pack(side = TOP)
#Various Labels
Label(frame, text = 'Full Name', width = 8).pack()
Label(frame, text = 'Education', width = 8).pack()
root.mainloop()
一些重新排列是有序的。您的大 frame
位于背景图片之上,完全覆盖了它。因此,让我们将背景 Label
设为 frame
的一部分,而不是 root
。您可能应该选择 place()
或 pack()
,但不能同时选择两者。为了让其他标签居中,我创建了一个居中框架并将它们打包到其中。可能还有其他方法可以完成所有这些:
from tkinter import *
from PIL import Image, ImageTk
def resize_image(event):
new_width = event.width
new_height = event.height
image = copy_of_image.resize((new_width, new_height))
photo = ImageTk.PhotoImage(image)
label.config(image=photo)
label.image = photo # avoid garbage collection
root = Tk()
root.title("Title")
root.geometry('600x600')
frame = Frame(root, relief='raised', borderwidth=2)
frame.pack(fill=BOTH, expand=YES)
frame.pack_propagate(False)
copy_of_image = Image.open("filepath.jpg")
photo = ImageTk.PhotoImage(copy_of_image)
label = Label(frame, image=photo)
label.place(x=0, y=0, relwidth=1, relheight=1)
label.bind('<Configure>', resize_image)
center_frame = Frame(frame, relief='raised', borderwidth=2)
center_frame.place(relx=0.5, rely=0.5, anchor=CENTER)
Label(center_frame, text='Full Name', width=8).pack()
Label(center_frame, text='Education', width=8).pack()
root.mainloop()