Python 在Tkinter和x2014中验证命令;循环验证?

Python 在Tkinter和x2014中验证命令;循环验证?,python,tkinter,tkinter-entry,Python,Tkinter,Tkinter Entry,这就是我目前所拥有的 vdcm = (self.register(self.checkForInt), '%S') roundsNumTB = Entry(self, validate = 'key', validatecommand = vdcm) 然后,checkForInt()函数的定义如下 def checkForInt(self, S): return (S.isDigit()) 该输入框应为偶数,且仅为一个数字;不是角色。如果输入了字符,则拒绝该字符。但这只会起作

这就是我目前所拥有的

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会话:

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
[1]中的
:def my_func():返回True
在[2]:my_func()
Out[2]:正确
在[3]:my_Func()
---------------------------------------------------------------------------
NameError回溯(最近一次呼叫上次)
在()
---->1我的_Func()
名称错误:未定义名称“my_Func”

它之所以能工作,是因为Python区分大小写,而且
isdigit()
isdigit()
不同。我会用解释更新答案。这解释了吗?或者你被模(%)运算符搞糊涂了?是的,我知道。因为我没有考虑到它是区分大小写的,所以口译员只是做了d(d使它成为动词或什么的)。太好了,很高兴能帮上忙。快乐编码!