I'm trying to add a background image to my GUI. I'm using tkraise()
to navigate through pages. I'm using a for loop to initialize "page" type objects and using the show_frame
method to raise each page to the top when clicking the respective button.
from tkinter import *
class App(Tk):
def __init__(self, *args, **kwargs):
Tk.__init__(self, *args, **kwargs)
#Setup Menu
MainMenu(self)
#Setup Frame
canvas = Canvas(self, height = 700, width = 800)
canvas.pack()
container = Frame(self, bg = '#80c1ff', bd = 5)
container.place(relx = 0.5, rely = 0.05, relwidth = 0.90, relheight = 0.90, anchor = 'n')
container.grid_rowconfigure(1, weight=1)
container.grid_columnconfigure(0, weight=1)
# container = Frame(self, bg = '#80c1ff', bd=5, height = 600, width = 600)
# container.place(relx = 0.5, rely = 0.05, relwidth = 0.90, relheight = 0.90, anchor = 'n')
self.frames = {}
for F in (StartPage, addItem, deleteItem, viewItem):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=1, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, context):
frame = self.frames[context]
frame.tkraise()
class StartPage(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
label = Label(self, text="Start Page", font = 40)
label.place(relx = 0.38, relheight = .08, rely = .10, relwidth = 0.25)
view_item = Button(self, text = "View Items", font = 40, command = lambda:controller.show_frame(viewItem))
view_item.place(relx = 0.38, relheight = .08, rely = .50 , relwidth = 0.25)
exit_window = Button(self, text = "Exit", font = 40, command = quit)
exit_window.place(relx = 0.38, relheight = .08, rely = .60 , relwidth = 0.25)
add_item = Button(self, text = "Add Item", font = 40, command=lambda:controller.show_frame(addItem))
add_item.place(relx = 0.38, relheight = .08, rely = .30 , relwidth = 0.25)
delete_item = Button(self, font = 40, text="Delete Item", command=lambda:controller.show_frame(deleteItem))
delete_item.place(relx = 0.38, relheight = .08, rely = .40 , relwidth = 0.25)
class addItem(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
start_page = Button(self, text="Start Page", command=lambda:controller.show_frame(StartPage))
start_page.pack()
class deleteItem(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
start_page = Button(self, text="Start Page", command=lambda:controller.show_frame(StartPage))
start_page.pack()
class viewItem(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
start_page = Button(self, text="Start Page", command=lambda:controller.show_frame(StartPage))
start_page.pack()
class MainMenu:
def __init__(self, master):
menubar = Menu(master)
filemenu = Menu(menubar, tearoff=0)
filemenu.add_command(label="Exit", command=master.quit)
menubar.add_cascade(label="File", menu=filemenu)
master.config(menu=menubar)
app = App()
app.mainloop()
I tried loading a gif using PhotoImage
and using place
on a label object but it's not showing anything. I used PhotoImage
below canvas.pack()
in the constructor.
Create a class for rendering the wallpaper, call the class on each page and pass in the parent widget self
for each class.
class BackGround:
def __init__(self,root):
self.canvas = Canvas(root)
self.canvas.place(relx=0.5, rely=0.5, relwidth=1, relheight=1, anchor='center')
self.image_path = Image.open("Path to image")
self.image = ImageTk.PhotoImage(self.image_path)
self.canvas.image = self.image # Keeping Reference to the Image
self.canvas.create_image(50, 10, image=self.image, anchor=NW)
So eg if you want class addItem(Frame):
to have a wallpaper do BackGround(self)
This is how it looks:
class addItem(Frame):
def __init__(self, parent, controller):
Frame.__init__(self, parent)
BackGround(self)
start_page = Button(self, text="Start Page", command=lambda: controller.show_frame(StartPage))
start_page.pack()
Same principal applies for other pages.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.