【问题标题】:Unable to close the popup for Tkinter after entering the correct password输入正确密码后无法关闭 Tkinter 的弹出窗口
【发布时间】:2021-02-10 05:23:19
【问题描述】:

输入正确密码后无法关闭 Tkinter 的弹出窗口。输入正确密码后,GUI 没有关闭。

from tkinter import *

root = Tk()

e = Entry(root, width=50)
e.pack()

def myClick():
    password = "sunny567"
    get = e.get()
    if get == password:
        myLabel = Label(root, text=get)
        myLabel.pack()

    else:
        myLabel = Label(root, text="Entered Password is wrong. Please try again.")
        myLabel.pack()

myButton = Button(root, text="Enter the password", command=myClick)
myButton.pack()

root.mainloop()

【问题讨论】:

    标签: python python-3.x tkinter tkinter-canvas tkinter-entry


    【解决方案1】:

    密码正确时需要拨打root.destroy()

    from tkinter import *
    
    root = Tk()
    
    e = Entry(root, width=50)
    e.pack()
    
    def myClick():
        password = "sunny567"
        get = e.get()
        if get == password:
            root.destroy()  # close the root window
        else:
            myLabel.config(text="Entered Password is wrong. Please try again.")
    
    myButton = Button(root, text="Enter the password", command=myClick)
    myButton.pack()
    
    myLabel = Label(root)
    myLabel.pack()
    
    root.mainloop()
    

    【讨论】:

    • 当我多次输入错误密码时,在提交按钮下方打印输入的密码错误。如何避免?
    • 我的回答中按钮下方应该只有一条消息。那么您实际想要的预期结果是什么?
    • 例如第一次输入密码错误,提示输入密码错误,请重试。如果您在第一次尝试输入错误密码的情况下再次输入错误密码,则与输入密码错误一样。如何避免?
    • 正如我所说,我的回答仅在按钮下方显示一条消息。您的将显示多条消息。
    猜你喜欢
    • 2015-01-25
    • 2015-07-16
    • 1970-01-01
    • 1970-01-01
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    • 2017-07-15
    • 1970-01-01
    相关资源
    最近更新 更多