Marvin
Marvin

Reputation: 53

How can I fix it so that the widget password_entry gets destroyed after right password?

I can't figure out why my widget password_entry doesn't get destroyed in the following code after you enter the right password (asdf). Any Ideas on how to fix that?

if you click on the first IMS you can enter the username (Marvin) and if it's correct you can enter the password (asdf). If the password is correct, you should get a red IMS and no entry left.

I don't understand why username_entry gets destroyed but password_entry doesn't. Thanks four your help!

from Tkinter import *
import time

title_active = False

root = Tk()
width_root = 1000
height_root = 600
root.geometry("1000x600+200+150")
rootcanvas = Canvas(root, width=1000, height=600)
rootcanvas.pack()

def create_title(color):
    global title_active, title_rect_length, title_rect_height, title_rect, title_text
    title_rect_length = 400
    title_rect_height = 100
    title_rect = rootcanvas.create_rectangle(width_root/2-title_rect_length/2, height_root/2-title_rect_height, width_root/2+title_rect_length/2, height_root/2+title_rect_height, width=5, outline=color)

    title_text = rootcanvas.create_text(width_root/2, height_root/2, text="IMS", font="Helvetica 200", fill=color)
    title_active = True

def check_password(event):
    global password_entry, password_text
    if password_entry.get() == "asdf":
        rootcanvas.delete(password_entry)
        rootcanvas.delete(password_text)
        create_title("red")
    else:
        print("password wrong")

def check_username(event):
    global password_text, password_entry, username_entry, username_text
    if username_entry.get() == "Marvin":
        rootcanvas.delete(username_entry)
        rootcanvas.delete(username_text)
        password_text = rootcanvas.create_text(width_root/2, height_root/2, text="Password", font="Helvetica 50", fill="blue")
        password_entry = Entry(rootcanvas)

        entry_x = width_root/2-110
        entry_y = height_root/2+20
        password_entry.place(x=entry_x, y= entry_y, width=220, height=40)
        password_entry.bind("<Return>", check_password)

    else:
        print("This user is unauthorized to enter IMS.")

def login_menu():
    global username_entry, username_text
    username_text = rootcanvas.create_text(width_root/2, height_root/2, text="Username", font="Helvetica 50", fill="blue")
    username_entry = Entry(rootcanvas)
    entry_x = width_root/2-110
    entry_y = height_root/2+20
    username_entry.place(x=entry_x, y= entry_y, width=220, height=40)
    username_entry.bind("<Return>", check_username)

def title_clicked():
    global title_active
    title_active = False
    rootcanvas.delete(title_rect)
    rootcanvas.delete(title_text)
    login_menu()

def click(event):
    global x,y, title_active
    x = event.x
    y = event.y
    if title_active == True:
        if width_root/2-title_rect_length/2 <= x <= width_root/2+title_rect_length/2 and height_root/2-title_rect_height <= y <= height_root/2+title_rect_height: 
            title_clicked()

rootcanvas.bind("<Button 1>", click)
create_title("blue")
root.mainloop()

Upvotes: 1

Views: 26

Answers (1)

I Funball
I Funball

Reputation: 380

Change "rootcanvas.delete(password_entry)" to "password_entry.destroy()" and the same for username_entry ("rootcanvas.delete(username_entry)" to "username_entry.destroy()"

This should fix your problem.

Upvotes: 1

Related Questions