root
root

Reputation: 192

Delete in Tkinter Python

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

Upvotes: 2

Views: 342

Answers (2)

AwesomeSam
AwesomeSam

Reputation: 163

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

Upvotes: 1

OctaveL
OctaveL

Reputation: 1045

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

Upvotes: 2

Related Questions