tkinter 条目验证,python 278
tkinter entry validate, python 278
我正在验证一些条目小部件,浏览 SO 和 google 上的各种帖子。修改了 Bryan O. 代码几乎可以工作。当输入文本是一位数时,验证失败 - 为什么?? 2个以上,成功。
我的更改是检查数字而不是小写字母。当然,原始代码确实如宣传的那样工作。
import Tkinter as tk
def onvalidate(d,i,P,s,S,v,V,W):
# only lowercase is valid
# valid = (P.lower() == P)
valid = P.isdigit()
print 'P is:', type(P), P
print 'valid is:', valid
# set red background if invalid
newcolor = 'red' if not valid else default_color
root.nametowidget(W).configure(background=newcolor)
return valid
def oninvalid(d,i,P,s,S,v,V,W):
#called if widget is invalid
widget = root.nametowidget(W)
# S is the character that *would* have been
# inserted or deleted, but won't because it is invalid
# So we do it ourselves
if S:
if d=='0':
widget.delete(i, len(S))
elif d=='1':
widget.insert(i, S)
# Changing the text clears the 'validate' value
# so we have to reset it
widget.after_idle(lambda W,v: root.nametowidget(W).configure(validate=v), W, v)
root = tk.Tk()
valhook = (root.register(onvalidate), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
invhook = (root.register(oninvalid), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
entry = tk.Entry(root, validate="key", validatecommand=valhook, invalidcommand=invhook)
default_color = entry.cget('background')
entry.pack()
方法 2(更简单的代码):仅适用于输入的第一个字符,其他失败。我知道 S 是当前字符,P 是允许编辑,但我很确定我想在这里检查 S。为什么它不适用于字段中输入的所有内容??
import Tkinter as tk
def OnValidate(d, i, P, s, S, v, V, W):
print "OnValidate:"
print "d='%s'" % d
print "i='%s'" % i
print "P='%s'" % P
print "s='%s'" % s
print "S='%s'" % S
print "v='%s'" % v
print "V='%s'" % V
print "W='%s'" % W
# only allow if the string is lowercase
# return (S.lower() == S)
print 'S is:', S, type(S)
if S.isdigit():
print 'Check:', S.isdigit()
return S
else:
pass
root = tk.Tk()
vcmd = (root.register(OnValidate),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
entry = tk.Entry(root, validate="key",
validatecommand=vcmd)
entry.pack()
root.mainloop()
您的验证函数 必须 return True
或 False
,它告诉 tkinter 是否允许编辑。如果它 return 是其他任何内容,验证将被关闭。
if S.isdigit():
return True
else:
False
我正在验证一些条目小部件,浏览 SO 和 google 上的各种帖子。修改了 Bryan O. 代码几乎可以工作。当输入文本是一位数时,验证失败 - 为什么?? 2个以上,成功。
我的更改是检查数字而不是小写字母。当然,原始代码确实如宣传的那样工作。
import Tkinter as tk
def onvalidate(d,i,P,s,S,v,V,W):
# only lowercase is valid
# valid = (P.lower() == P)
valid = P.isdigit()
print 'P is:', type(P), P
print 'valid is:', valid
# set red background if invalid
newcolor = 'red' if not valid else default_color
root.nametowidget(W).configure(background=newcolor)
return valid
def oninvalid(d,i,P,s,S,v,V,W):
#called if widget is invalid
widget = root.nametowidget(W)
# S is the character that *would* have been
# inserted or deleted, but won't because it is invalid
# So we do it ourselves
if S:
if d=='0':
widget.delete(i, len(S))
elif d=='1':
widget.insert(i, S)
# Changing the text clears the 'validate' value
# so we have to reset it
widget.after_idle(lambda W,v: root.nametowidget(W).configure(validate=v), W, v)
root = tk.Tk()
valhook = (root.register(onvalidate), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
invhook = (root.register(oninvalid), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
entry = tk.Entry(root, validate="key", validatecommand=valhook, invalidcommand=invhook)
default_color = entry.cget('background')
entry.pack()
方法 2(更简单的代码):仅适用于输入的第一个字符,其他失败。我知道 S 是当前字符,P 是允许编辑,但我很确定我想在这里检查 S。为什么它不适用于字段中输入的所有内容??
import Tkinter as tk
def OnValidate(d, i, P, s, S, v, V, W):
print "OnValidate:"
print "d='%s'" % d
print "i='%s'" % i
print "P='%s'" % P
print "s='%s'" % s
print "S='%s'" % S
print "v='%s'" % v
print "V='%s'" % V
print "W='%s'" % W
# only allow if the string is lowercase
# return (S.lower() == S)
print 'S is:', S, type(S)
if S.isdigit():
print 'Check:', S.isdigit()
return S
else:
pass
root = tk.Tk()
vcmd = (root.register(OnValidate),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
entry = tk.Entry(root, validate="key",
validatecommand=vcmd)
entry.pack()
root.mainloop()
您的验证函数 必须 return True
或 False
,它告诉 tkinter 是否允许编辑。如果它 return 是其他任何内容,验证将被关闭。
if S.isdigit():
return True
else:
False