0

I'm new to python and got stuck while trying to build a GUI. I can't find a way to extract data from the 'login' function, which would be the new TopLevel window created after the user logs in. Because of that, I have to write the remaining code inside the 'login function', but I have the impression that there must be another way around. I tried making the new top level global, but it returns that the new variable is not defined.

from tkinter import *  
from tkinter import messagebox

root = Tk()

login_frame = LabelFrame(root, text = "login info").pack()
user_field = Label(login_frame, text = "user: ")
user_field.grid(row = 0,column = 0)
pass_field = Label(login_frame, text = "pass: ")
pass_field.grid(row = 1, column = 0)
user_input = Entry(login_frame)
user_input.grid(row = 0, column = 1)
pass_input = Entry(login_frame, show = "*")
pass_input.grid(row = 1, column = 1)

def login():
if user_input.get() == "user" and pass_input.get() == "user":
    if messagebox.showinfo("blah", "blah") == "ok":
        pass_input.delete(0, END)
        user_input.delete(0, END)
        root.withdraw()
        **app = Toplevel()**

else:
    messagebox.showerror("blah", "blah")
    pass_input.delete(0, END)
    user_input.delete(0, END)

login_btn = Button(login_frame, text = "LOGIN")
login_btn.grid(row = 2, column = 0)
exit_btn = Button(login_frame, text = "SAIR")
exit_btn.grid(row = 2, column = 1)    

root.mainloop()
Stidgeon
  • 2,673
  • 8
  • 20
  • 28
  • You can put the task after login successful in another function, then call this function in `login()`. – acw1668 Mar 30 '20 at 06:28
  • ***"which would be the new TopLevel window created after the user logs in"***: Wrong approach, the login have to be the `Toplevel()` or a `Frame` in the root window. Read [Switch between two frames](https://stackoverflow.com/a/7557028/7414759) – stovfl Mar 30 '20 at 08:02
  • Thanks guys, I managed to solve this problem by following your tips – Vinícius Lima Apr 06 '20 at 22:21

1 Answers1

0

Your code is breaking indentation. The lines following the definition of the function must be inside the scope of the function, like this:

def login():
    if user_input.get() == "user" and pass_input.get() == "user":
        if messagebox.showinfo("blah", "blah") == "ok":
            ...

Regardless of that, you may return any type of data at the end of a function. Consider exposing your TopLevel app like this:

return TopLevel()
phramos07
  • 136
  • 1
  • 8
  • If `login()` is triggered by clicking the `LOGIN` button (i.e. `command=login`), then its return value is ignored. – acw1668 Mar 30 '20 at 06:26