I have already done some small things with Tkinter for college assignments, but now i am writing this GUI interface for a desktop app as a final assignement. So i tried wrinting the code for it to go inbetween frames and not pop-up frames. And somehow the using an image as a button is not functioning and i have tried most of the things i know and even more.
Can somebody help me? Here's the code i have the far. As an example of the (un)working button we have one on the class PageOne:
import tkinter as tk
from tkinter import *
from tkinter.ttk import *
#Fontes Constantes
LARGE_FONT = ("Verdana", 12)
#Base line code that is necessary for adding pages
class FoodRecipes(tk.Tk): #inherit tk.Tk tk class within Tkinter
def __init__(self,*args, **kwargs ): #init is really a method not a function - is going to initalize upon with the class (whenever the class is called)
tk.Tk.__init__(self, *args, **kwargs) #initialize tkinter too
tk.Tk.iconbitmap(self, default="assets\pngegg.ico")
tk.Tk.wm_title(self, "Food Recipes")
tk.Tk.wm_geometry(self, "1440x1024")
container = tk.Frame(self) #frame is a part of tkinter, what you i'll think as a window
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1) #0 is min size; weight who gets priority
self.frames = {}
for F in (StartPage, PageOne, PageTwo):
frame = F(container, self) #initial page
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew") #stretch everything to the sides of that window
self.show_frame(StartPage) #pull/starts StartPage always first
def show_frame(self, cont): #cont = controlller
frame = self.frames[cont] #self.frames is all the frames - cont is the key, looking for the value of self.frames with this key. and whatever that is is being thrown in the frame and we're saying that .tkraise()
frame.tkraise() #going to raise it to the front - raise one up to the front. there's no real order here, whatevers behind the frontpage is being hidden
class StartPage(tk.Frame): #any tkinter window
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent) #parent class - Food Recipes, that is our main class here
label = tk.Label(self, text="Start Page", font=LARGE_FONT) #Label is the class and i created the object it is in
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Visit Page One",
command= lambda: controller.show_frame(PageOne)) #PageOne is a class
button1.pack()
button2 = tk.Button(self, text="Visit Page Two",
command= lambda: controller.show_frame(PageTwo)) #PageOne is a class
button2.pack()
class PageOne(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page One", font=LARGE_FONT) #Label is the class and i created the object it is in
label.pack(pady=10,padx=10)
# Creating a photoimage object to use image
photo = PhotoImage(file = r"assets/frame0/button_allrecipes.png")
# here, image option is used to
# set image on button
button_photo = Button(self, text = 'Click Me !', image = photo).pack(side = TOP)
class PageTwo(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text="Page Two", font=LARGE_FONT) #Label is the class and i created the object it is in
label.pack(pady=10,padx=10)
button1 = tk.Button(self, text="Back to Home",
command= lambda: controller.show_frame(StartPage)) #PageOne is a class
button1.pack()
button2 = tk.Button(self, text="Back to Page One",
command= lambda: controller.show_frame(PageOne)) #PageOne is a class
button2.pack()
app = FoodRecipes()
app.resizable(False, False)
app.mainloop()