0
class Login(tk.Frame):

    def __init__(self, parent, controller):
        def switch():
            e_name.delete(0, 'end')
            e_password.delete(0, 'end')
            controller.show_frame("Registration")

        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(bg=white)
        
        Title = tk.Label(self, text="Login", font=('Verdana 17 bold'), height = 1, width=20, anchor='nw', bg=seafoam, fg=white)
        Title.pack(side="top", fill="x", pady=10)
        
        def login():
            global name
            name = e_name.get()
            password = e_password.get()
            if name == '' or password == '':
                messagebox.showwarning('data', 'Please fill in all fields')
                e_name.delete(0,"end")
                e_password.delete(0, "end")
            else:
                A = login_user(name, password)
                if A == True:
                    e_name.delete(0,"end")
                    e_password.delete(0, "end")
                    controller.show_frame('Application')
                else:
                    e_name.delete(0,"end")
                    e_password.delete(0, "end")
                    messagebox.showwarning('data', 'Wrong Name or Password')
                
        t_name = tk.Label(self, text="Name *", width=5, height=1, font=('Ivy 10'), bg=white, anchor='nw')
        t_name.place(x=10, y=50)
        e_name = tk.Entry(self, width=25, justify='left', highlightthickness=1, relief="solid")
        e_name.place(x=100, y=50)
        
        l_password = tk.Label(self, text="Password *", height=1, font=('Ivy 10'), bg=white, anchor='nw')
        l_password.place(x=10, y=110)
        e_password = tk.Entry(self, width=25, justify='left', highlightthickness=1, relief="solid", show = "*")
        e_password.place(x=100, y=110)

        button1 = tk.Button(self, text="Login",
                             command=login, bg=seafoam, fg=white, font=('Ivy 10'))
        button1.place(x=10, y=170)

        button2 = tk.Button(self, text="Register here",
                           command=switch, bg=seafoam, fg=white, font=('Ivy 10'), relief='ridge')
        button2.place(x=350, y=15)


class Application(tk.Frame):

    def __init__(self, parent, controller):
        def switch():
            e_name.delete(0, 'end')
            e_telephone.delete(0, 'end')
            e_email.delete(0, 'end')
            e_location.delete(0, 'end')

            controller.show_frame("Login")
        tk.Frame.__init__(self, parent)
        self.controller = controller
        self.configure(bg=white)
        Title = tk.Label(self, text="Phonebook", font=('Verdana 17 bold'), height = 1, width=20, anchor='nw', bg=seafoam, fg=white)
        Title.pack(side="top", fill="x", pady=10)
        button = tk.Button(self, text="Logout",
                           command=switch, bg=seafoam, fg=white, font=('Ivy 8 bold'), relief='ridge', width=10, height=1)
        button.place(x=400, y=15)

        user = tk.Label(self, text=name)
        user.place(x=300, y=15

I need the 'name' from the Login frame to pass to the Application frame. I have tried using global variables but that does not seem to be working. I mentioned Global variables outside the Login page as well inside it, but both don't work. I use the frame switching code from this Switch between two frames in tkinter?.

Thanks in advance.

  • `name` is a local variable inside `login()`, so it cannot be accessed elsewhere. Declaring it as global inside `login()` should make it be available in other function as long as `login()` has been executed once. – acw1668 May 05 '22 at 00:43
  • I tried that but when i ask the Application frame to print 'name' it says it's not defined. – Vasu Mahajan May 05 '22 at 06:02
  • As I said, is `login()` executed at least once before accessing `name` inside `Application` class? – acw1668 May 05 '22 at 06:19
  • Yes, first the login screen comes up and you have to login before actually entering the application frame. – Vasu Mahajan May 05 '22 at 10:31
  • I have tested your code using global variable (add missing parts to make it a [mre]) and it works. So better show what you have tried on using global variable by adding the updated code. – acw1668 May 05 '22 at 10:47
  • Would you be able to post the code that you tested here? – Vasu Mahajan May 05 '22 at 10:53
  • I have edited how i used global variable – Vasu Mahajan May 05 '22 at 11:26
  • Are the last two lines within the function `Application.__init__()`? If yes, then you access the variable `name` before `login()` is executed because `user = tk.Label(self, text=name)` will be executed when instance of `Application` is created. – acw1668 May 05 '22 at 11:35
  • Yes, its in the application frame – Vasu Mahajan May 05 '22 at 11:56

0 Answers0