'How to make a background image not cover labels in tkinter
I am making a simple maths game but my background is covering the labels
how do I make it so the background image still resizes to the screen but is under the labels instead?
this is my code:
import tkinter
from tkinter import *
import tkinter.font as font
from PIL import Image, ImageTk
# INITIALIZE TKINTER
root = tkinter.Tk()
root.title('Test De Multiplications')
#root.iconbitmap('')
root.geometry('600x400')
# DEFINE FONTS
labelfont = font.Font(family='Comic Sans MS', weight='bold', size='30')
normalfont = font.Font(family='Arial', size='20')
subtitle = font.Font(family='Calibri', weight='bold', size='25')
# RAISING FRAMES (Swithching windows)
def raise_frame(frame):
frame.tkraise()
f1 = Frame(root)
f2 = Frame(root)
for frame in (f1, f2):
frame.grid(row=0, column=0, sticky='news')
title = Label(f1, text="TEST DE MULTIPLICATIONS", font=labelfont)
title.pack(side= TOP, fill = BOTH, expand = True)
start = Button(f1, text='Commencer', font=normalfont, background='darkred', command=lambda:raise_frame(f2))
start.pack(side= TOP, expand= True)
playtitle = Label(f2, text='QUESTION:', font=normalfont ).pack()
raise_frame(f1)
class bg(Frame):
def __init__(self, master, *pargs):
Frame.__init__(self, master, *pargs)
self.image = Image.open("bg.ppm")
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)
e = bg(root)
e.place(x=0, y=0, relwidth=1, relheight=1)
root.mainloop()
I'm using multiple frames for the window switching so I'm wondering if that has something to do with it? I still want to be able to change windows like this, because it works well.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
