Tkinter 条目多次验证
Tkinter Entry Multiple Vaildations
我正在尝试对键 和 focusout 进行验证,以便将输入限制为允许的字符
我可以在用户聚焦时调用一个函数(留下有效条目)。
这样的事情可能吗?
import tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
vcmd = (self.register(self.on_validate), '%S')
vcmd2 = (self.register(self.if_valid), '%s')
# This is the meat of the question; looking to do something like
self.entry = tk.Entry(self,
validate="key",
validate2="focusout",
validatecommand=vcmd,
validate2command=vcmd2)
self.text = tk.Text(self, height=10, width=40)
self.entry.pack(side="top", fill="x")
self.text.pack(side="bottom", fill="both", expand=True)
def on_validate(self, S):
# Disallow anything but binary string
if S == '1' or '0':
return True
else:
self.bell()
return False
def if_valid(self, s):
if len(s) == 3:
self.my_function(s)
return True
else:
return False
def my_function(self, value):
# send value somewhere
pass
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
取自:https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/entry-validation.html
也许您想使用 all?
'focus' Validate whenever the Entry widget gets or loses focus (see
Section 53, “Focus: routing keyboard input”).
'focusin' Validate whenever the widget gets focus.
'focusout' Validate whenever the widget loses focus.
'key' Validate whenever any keystroke changes the widget's contents.
'all' Validate in all the above situations.
'none' Turn off validation. This is the default option value. Note
that this is the string 'none', not the special Python value None.
如果您必须只验证 focusout
和 key
,那么您可以使用 all
,然后在您的验证方法中您可以简单地调用 .get_focus()
查看焦点是否在框上的方法。
我正在尝试对键 和 focusout 进行验证,以便将输入限制为允许的字符 我可以在用户聚焦时调用一个函数(留下有效条目)。
这样的事情可能吗?
import tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
vcmd = (self.register(self.on_validate), '%S')
vcmd2 = (self.register(self.if_valid), '%s')
# This is the meat of the question; looking to do something like
self.entry = tk.Entry(self,
validate="key",
validate2="focusout",
validatecommand=vcmd,
validate2command=vcmd2)
self.text = tk.Text(self, height=10, width=40)
self.entry.pack(side="top", fill="x")
self.text.pack(side="bottom", fill="both", expand=True)
def on_validate(self, S):
# Disallow anything but binary string
if S == '1' or '0':
return True
else:
self.bell()
return False
def if_valid(self, s):
if len(s) == 3:
self.my_function(s)
return True
else:
return False
def my_function(self, value):
# send value somewhere
pass
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
取自:https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/entry-validation.html 也许您想使用 all?
'focus' Validate whenever the Entry widget gets or loses focus (see Section 53, “Focus: routing keyboard input”).
'focusin' Validate whenever the widget gets focus.
'focusout' Validate whenever the widget loses focus.
'key' Validate whenever any keystroke changes the widget's contents.
'all' Validate in all the above situations.
'none' Turn off validation. This is the default option value. Note that this is the string 'none', not the special Python value None.
如果您必须只验证 focusout
和 key
,那么您可以使用 all
,然后在您的验证方法中您可以简单地调用 .get_focus()
查看焦点是否在框上的方法。