I'm trying to create my first GUI, using Python and Tkinter. I want to have a background image that resizes accordingly with the window size, along with two labels on top of the background, both placed about midway on the window. The two labels are "Full Name" and "Education", as shown in my code below.
Currently, I'm using the pack() method, and I've been using the window resizing code from here.
My question is: How do I get the labels to overlap the background image (also a label in my code)? With my current code, the background image seems to be on top of the frame and labels.
Attached is a picture of the output/GUI I'm looking for, except I want my image to be in the background.
#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()
Some rearrangement is in order. Your large frame
is sitting atop the background image, covering it completely. So, let's make the background Label
part of frame
, not root
. You should probably either place()
or pack()
but not both. To get the other labels to the center, I created a centered frame and packed them in to it. There are probably other ways to do all of this:
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()