Python Forum
[Tkinter] Unable to Access global Variable when switching between frames
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Unable to Access global Variable when switching between frames
#1
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?

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()
Reply


Messages In This Thread
Unable to Access global Variable when switching between frames - by tziyong - Nov-02-2019, 12:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Issue with GUI screen switching in Python QT5 code greenelephant 1 496 Apr-04-2024, 09:58 PM
Last Post: deanhystad
  Switching from tkinter to gtk is difficult! snakes 1 1,555 Aug-08-2022, 10:35 PM
Last Post: woooee
  [Tkinter] tkinter global variable chalao_adda 6 11,392 Nov-14-2020, 05:37 AM
Last Post: chalao_adda
  Global Variable in Event Function? What am I doing wrong here? p_hobbs 1 3,622 Nov-13-2019, 02:50 PM
Last Post: Denni
  [Tkinter] Call a function when switching layouts 4096 0 3,624 Sep-22-2019, 07:39 PM
Last Post: 4096
  [PyGUI] Switching between two frames using Tkinter jenkins43 1 4,719 Jul-17-2019, 10:53 AM
Last Post: metulburr
  [Tkinter] switching frames nick123 2 8,067 Apr-18-2019, 04:50 PM
Last Post: francisco_neves2020
  tkinter - global variable not working albry 2 7,168 Jan-26-2019, 04:22 AM
Last Post: Larz60+
  Problem with pygame not switching the mouse image Mondaythe1st 6 6,968 Jul-26-2017, 10:53 PM
Last Post: metulburr

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020