gpt4 book ai didi

python - Tkinter 中的 validatecommand — 循环验证?

转载 作者:行者123 更新时间:2023-11-30 23:31:26 25 4
gpt4 key购买 nike

这就是我目前所拥有的

vdcm = (self.register(self.checkForInt), '%S')
roundsNumTB = Entry(self, validate = 'key', validatecommand = vdcm)

然后checkForInt()函数定义如下

def checkForInt(self, S):
return (S.isDigit())

输入框只能输入偶数,并且只能输入数字;不是字符。如果输入了字符,则会被拒绝。但这只会起作用一次。如果输入了字符,则不会拒绝作为输入的下一次击键。

如果有人能告诉我如何永久检查以确保字符串是数字,并且是偶数,我将不胜感激。

这是我收到的错误消息,如果有任何帮助

Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
return self.func(*args)
File "[py directory]", line 101, in checkForInt
return (S.isDigit())
AttributeError: 'str' object has no attribute 'isDigit'

最佳答案

我认为函数调用是 isdigit() 而不是 isDigit(),请注意大小写差异。如果您想测试输入是否为整数且为偶数,则必须首先使用 int() 转换字符串并测试:

def checkForEvenInt(self, S):
if S.isdigit():
if int(S) % 2 is 0:
return True
return False

请记住,Python 非常区分大小写,包括函数。例如,这是一个 iPython session :

In [1]: def my_func(): return True

In [2]: my_func()
Out[2]: True

In [3]: my_Func()
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-25-ac6a0a3aba88> in <module>()
----> 1 my_Func()

NameError: name 'my_Func' is not defined

关于python - Tkinter 中的 validatecommand — 循环验证?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19941393/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com