Nov-02-2019, 12:06 PM
Hi all I have tkinter code involving multiple frames. My goal is to have PageOne's label show the text of the button clicked in StartPage.
I created a function to change the global variable and have PageOne's class access the global variable after clicking on the button in StartPage. However, the frame in PageOne does not show the label even when the variable is changed. I am unsure of how to resolve this issue. Does the error due to my main class MiniProject __init__ function?
I created a function to change the global variable and have PageOne's class access the global variable after clicking on the button in StartPage. However, the frame in PageOne does not show the label even when the variable is changed. I am unsure of how to resolve this issue. Does the error due to my main class MiniProject __init__ function?
import tkinter as tk var = "" def changename(name): #function for changing global variable global var var = name return var class MiniProject(tk.Tk): #baseline code for adding/switching pages https://pythonprogramming.net/change-show-new-frame-tkinter/ def __init__(self, *args, **kwargs): #takes in any argument /keyword arguments tk.Tk.__init__(self, *args, *kwargs) container = tk.Frame(self) container.pack(side="top", fill="both", expand = True) container.grid_rowconfigure(0, weight=1) container.grid_columnconfigure(0, weight=1) self.frames = {} for F in (StartPage, PageOne): frame = F(container, self) self.frames[F] = frame frame.grid(row=0, column=0, sticky="nsew") self.show_frame(StartPage) def show_frame(self, cont): frame = self.frames[cont] frame.tkraise() class StartPage(tk.Frame): #Front page with buttons 1 to 5 def __init__(self, parent, controller): tk.Frame.__init__(self, parent) for x in [1,2,3,4,5]: #clicking button should bring me to PageOne with label as button number instead of "..." button = tk.Button(self, text=x, \ command= lambda j = x: [changename(name = j),controller.show_frame(PageOne)]) #code that changes variable to button number button.pack() class PageOne(tk.Frame): def __init__(self, parent, controller): global var tk.Frame.__init__(self, parent) label = tk.Label(self, text=var) #Supposed to display button number label.pack(pady=10,padx=10) button1 = tk.Button(self, text="TO HOME", command= lambda: controller.show_frame(StartPage)) button1.pack() app = MiniProject(var) app.mainloop()