如何解决该问题,以便在使用正确的密码后销毁小部件password_entry?

时间:2019-01-14 21:17:58

标签: python python-3.x tkinter widget

在输入正确的密码(asdf)后,以下代码中为什么我的小部件password_entry不会被销毁,我无法弄清楚。 关于如何解决该问题的任何想法?

如果单击第一个IMS,则可以输入用户名(Marvin),如果正确,则可以输入密码(asdf)。如果密码正确,您将获得一个红色的IMS,并且没有任何输入。

我不明白为什么username_entry会被销毁,而password_entry不会。 感谢四个您的帮助!

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

1 个答案:

答案 0 :(得分:1)

将“ rootcanvas.delete(password_entry)”更改为“ password_entry.destroy()”,并将username_entry的名称更改为相同的名称(将“ rootcanvas.delete(username_entry)”更改为“ username_entry.destroy()

这应该可以解决您的问题。