0

I'm making an application using tkinter, and I want to access global variable that I assign a value to from a tkinter Entry by a user. I'm following an MVC pattern for the creation of the application, and every window is in it's own class and I'm struggling basically with using data from one class to the next. In this context, I want to print the username entered by the user into the label in the class PageThree I've made a minimum reproduceable version of the code here:

import tkinter as tk
from tkinter import font as tkfont
from tkinter import messagebox


stored_username = "Tessa"


class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)

        self.title_font = tkfont.Font(family='Helvetica', size=18, weight="bold", slant="italic")


        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 (PageTwo, PageThree):
            page_name = F.__name__
            frame = F(parent=container, controller=self)
            self.frames[page_name] = frame


            frame.grid(row=0, column=0, sticky="nsew")

        self.show_frame("PageTwo")

    def show_frame(self, page_name):
        '''Show a frame for the given page name'''
        frame = self.frames[page_name]
        frame.tkraise()


class PageTwo(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self, parent)
        self.controller = controller

        self.label_username_signup = tk.Label(self, text="Username")
        self.label_password_signup = tk.Label(self, text="Password")

        self.entry_username_signup = tk.Entry(self)
        self.entry_password_signup = tk.Entry(self, show="*")

        self.label_username_signup.grid(row=0, sticky=tk.E)
        self.label_password_signup.grid(row=1, sticky=tk.E)
        self.entry_username_signup.grid(row=0, column=1)
        self.entry_password_signup.grid(row=1, column=1)

        self.rgbtn = tk.Button(self, text="Register", command=self.log_details)
        self.rgbtn.grid(columnspan=3)

        button = tk.Button(self, text="Back",
                       command=lambda: controller.show_frame("StartPage"))
        button.grid()

    def log_details(self):
        username = self.entry_username_signup.get()
        global stored_username
        stored_username = username
        if username:
            self.controller.show_frame("PageThree")
        else:
            tk.messagebox.showerror("Login Failure", "Incorrect username or password, please try again")


class PageThree(tk.Frame):

    def __init__(self, parent, controller):
        tk.Frame.__init__(self,parent)
        self.controller = controller
        global stored_username
        lbl = tk.Label(self,text=name_entry)
        lbl.grid(row=0,column=0)


def name_entry():
    global stored_username
    string = stored_username.get()
    return string


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()

I think the closest thing to a related question I can find is: Taking input from the user in Tkinter However they are not dealing with modular classes.

  • As long as you are not modifying the variable, you do not need to declare it global, you can use it if it is in a wider scope. – Reblochon Masque Apr 06 '20 at 14:54
  • I wish to modify the variable stored_username by changing it's value to whatever the user inputs into the entry_username_signup value when the user clicks the rgbtn Button in the clas PageTwo, and I want to output that value in the lbl Label in class PageThree – Odre2Py Apr 06 '20 at 14:57
  • A suggestion:`stored_username` is a `string` type.So `stored_username.get()` might raised the error:`'str' has no attribute .get` – jizhihaoSAMA Apr 06 '20 at 15:01
  • Removing the .get() doesn't seem to change anything when I run it, but thank you for the input – Odre2Py Apr 06 '20 at 15:04

0 Answers0