2

I'm trying to make a login system in Tkinter using Python. So the way I want to do it is by asking the user for a username. When they click next the username field should dissapear and a new field named password should appear instead. So when I tried to do this I get an error:

e1 is not defined in line 27, in button

I understand that the variable has not been declared yet, but it should have done it, as I have put everything in a main() function.
This is my code

from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("Loginsystem")
root.geometry("300x200")

def main():
    def username_page():
        Label(root, text="Username").place(x=10, y=10)
        e1 = Entry(root)
        e1.place(x=140, y=10)
        uname = e1.get()

    def password_page():
        Label(root, text="Password").place(x=10, y=40)
        e2 = Entry(root)
        e2.place(x=140, y=40)
        e2.config(show="*")

    def button():
        #Hide the username input field
        e1.delete()
        #Show the password input field
        password_page()


    username_page()

    Button(root, text="Next", command=button, height = 3, width = 35, background = "blue", foreground = "white").place(x=10, y=100)

    root.mainloop()


main()

2 Answers 2

2
from tkinter import *
from tkinter import messagebox

root = Tk()
root.title("Loginsystem")
root.geometry("300x200")


def main():
    e1 = Entry(root) # MOVED E1 HERE

    def username_page():
        Label(root, text="Username").place(x=10, y=10)
        e1.place(x=140, y=10)
        uname = e1.get()

    def password_page():
        Label(root, text="Password").place(x=10, y=40)
        e2 = Entry(root)
        e2.place(x=140, y=40)
        e2.config(show="*")

    def button():
        #Hide the username input field
        e1.place_forget()
        #Show the password input field
        password_page()

    username_page()
    Button(root, text="Next", command=button, height = 3, width = 35, background = "blue", foreground = "white").place(x=10, y=100)
    root.mainloop()


main()

e1 was declared in the scope of the username_page() function, so it was only accessible in it and not outside. The fact that username_page() was declared inside main() doesn't mean e1 was declared in the scope of your main().

Also, e1.delete() doesn't work, I believe you want e1.place_forget(). When you want e1 to be visible again, you can call .place() like you did in username_page().

Sign up to request clarification or add additional context in comments.

5 Comments

e1.delete() or e1.delete(0) is not hiding the Entry
@SamakshGupta You're right, my mistake. I'll edit it.
Uhh, I am sorry but the code says to hide, destroying isn't a alternative to hide. Maybe use .place_forget()?
@SamakshGupta Correct again, I wasn't aware of it! Thanks. I'd delete my answer to leave yours, since our programs are almost identical, but I dislike using globals in nearly every situation, so I'd rather have both up for OP to choose.
Haha, yes our answers are pretty same, but I am afraid that you answered first, so you shouldn't delete it :)
1

There is a very easy way to do it, without moving lines here and there, i.e. declaring e1 as Global.

Also, hiding doesn't mean deleting, use e1.place_forget() to hide the widget

Here is the code which works:

from tkinter import *

root = Tk()
root.title("Loginsystem")
root.geometry("300x200")

def username_page():
    global e1, myLabel
    myLabel = Label(root, text="Username")
    myLabel.place(x=10, y=10)
    e1 = Entry(root)
    e1.place(x=140, y=10)
    uname = e1.get()

def password_page():
    Label(root, text="Password").place(x=10, y=40)
    e2 = Entry(root)
    e2.place(x=140, y=40)
    e2.config(show="*")


def button():
    # Hide the username input field
    e1.place_forget()
    myLabel.place_forget()
    # Show the password input field
    password_page()

def main():
    username_page()
    Button(root, text="Next", command=button, height = 3, width = 35, background = "blue", foreground = "white").place(x=10, y=100)
    root.mainloop()

main()

2 Comments

Cool! That works, but it does not delete the username text. Is there a similar way to do that?
@lukas Answer Updated to include it :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.