(Python)当密码不匹配时,禁用TKINTER中的按钮

发布于 2025-02-07 11:59:21 字数 1404 浏览 2 评论 0原文

当两个密码不匹配时,如何禁用TKInter窗口中的按钮?

我的工作:

from tkinter import *
from functools import partial

root = Tk()
root.geometry('280x100')
root.title('Tkinter Password')

def validation_pw(ep,cp):
    if ep.get() == cp.get():
        Label(root, text = "Confirmed").grid(column=0, row=5)
    else:
        Label(root, text = "Not matched").grid(column=0, row=5)
#         check_button['state'] = DISABLED    <============================
    
ep = StringVar()
cp = StringVar()

Label1 = Label(root, text = "Enter Password").grid(column=0, row=0)
pwEnty = Entry(root, textvariable = ep, show = '*').grid(column=1, row=0) 

# Confirmed password label
Label2 = Label(root, text = "Confirm Password").grid(column=0, row=1)
pwconfEnty = Entry(root, textvariable = cp, show = '*').grid(column=1, row=1)

validation_pw = partial(validation_pw, ep,cp)

check_button = Button(root, text = "check", command = validation_pw).grid(column=0, row=4)

root.mainloop()

它显示了是否匹配两个密码。

现在,如果两个密码不匹配,我想禁用检查按钮。我希望用户在失败时不能再尝试密码了。
因此,在函数validation_pw中,我添加check_button ['state'] = disabled
但是,错误会弹出

typeError:'nontype'对象不支持项目分配

如何解决此问题?谢谢!

How to disable the button in tkinter window when two passwords does not match?

My work:

from tkinter import *
from functools import partial

root = Tk()
root.geometry('280x100')
root.title('Tkinter Password')

def validation_pw(ep,cp):
    if ep.get() == cp.get():
        Label(root, text = "Confirmed").grid(column=0, row=5)
    else:
        Label(root, text = "Not matched").grid(column=0, row=5)
#         check_button['state'] = DISABLED    <============================
    
ep = StringVar()
cp = StringVar()

Label1 = Label(root, text = "Enter Password").grid(column=0, row=0)
pwEnty = Entry(root, textvariable = ep, show = '*').grid(column=1, row=0) 

# Confirmed password label
Label2 = Label(root, text = "Confirm Password").grid(column=0, row=1)
pwconfEnty = Entry(root, textvariable = cp, show = '*').grid(column=1, row=1)

validation_pw = partial(validation_pw, ep,cp)

check_button = Button(root, text = "check", command = validation_pw).grid(column=0, row=4)

root.mainloop()

It shows if two passwords are not matched.

enter image description here

Now, I want to disable the check button if two passwords are not matched. I want the user cannot try the passwords anymore if failure.
So in the function validation_pw, I add check_button['state'] = DISABLED.
However, an error pops out

TypeError: 'NoneType' object does not support item assignment

How to fix this issue? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

橘香 2025-02-14 11:59:21

您的Checkbutton实际上是,因为它是网格函数的结果。

要修复它,请首先声明按钮,然后将其网格网格。

之前:

check_button = Button([...]).grid(column=0, row=4) # Result of Button.grid function
print(check_button) # None

之后:

check_button = Button([...])
check_button.grid(column=0, row=4)
print(check_button) # Button object ...

Your checkbutton is actually None, because it's the result of the grid function.

To fix it, first declare the button, next grid it.

Before:

check_button = Button([...]).grid(column=0, row=4) # Result of Button.grid function
print(check_button) # None

After:

check_button = Button([...])
check_button.grid(column=0, row=4)
print(check_button) # Button object ...
寒江雪… 2025-02-14 11:59:21

您会收到nonepy的错误,因为在某一时刻,它没有分配。这是因为您在与按钮同一行上使用了.grid
固定代码:

check_button = Button(root, text = "check", command = validation_pw)
check_button.grid(column=0, row=4)

You get the error of NoneType because at one point it was assigned nothing. This is because you used .grid on the same line as the button.
Fixed code:

check_button = Button(root, text = "check", command = validation_pw)
check_button.grid(column=0, row=4)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文