[英]Interactively validating Entry widget content in tkinter (part 2 - change the properties of the entry object)
[英]Interactively validating Entry widget content in tkinter
在 tkinter Entry
小部件中交互式验证内容的推荐技术是什么?
我已经阅读了有关使用validate=True
和validatecommand=command
的帖子,并且这些功能似乎受到以下事实的限制:如果validatecommand
命令更新了Entry
小部件的值,它们就会被清除。
鉴于这种行为,我们是否应该绑定KeyPress
、 Cut
和Paste
事件并通过这些事件监视/更新Entry
小部件的值? (以及我可能错过的其他相关事件?)
或者我们应该完全忘记交互式验证,只对FocusOut
事件进行验证?
正确答案是,使用小部件的validatecommand
属性。 不幸的是,这个特性在 Tkinter 世界中被严重记录不足,尽管它在 Tk 世界中被充分记录。 即使它没有很好地记录,它也有您进行验证所需的一切,而无需借助绑定或跟踪变量,或在验证过程中修改小部件。
诀窍是要知道您可以让 Tkinter 将特殊值传递给您的 validate 命令。 这些值为您提供了决定数据是否有效所需的所有信息:编辑前的值、编辑后的值(如果编辑有效)以及其他一些信息。 但是,要使用这些,您需要做一些巫术来将此信息传递给您的验证命令。
注意:验证命令返回True
或False
很重要。 其他任何事情都会导致小部件的验证被关闭。
这是一个只允许小写的例子。 出于说明目的,它还打印所有特殊值的值。 它们并非都是必需的。 你很少需要超过一两个。
import tkinter as tk # python 3.x
# import Tkinter as tk # python 2.x
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
# valid percent substitutions (from the Tk entry man page)
# note: you only have to register the ones you need; this
# example registers them all for illustrative purposes
#
# %d = Type of action (1=insert, 0=delete, -1 for others)
# %i = index of char string to be inserted/deleted, or -1
# %P = value of the entry if the edit is allowed
# %s = value of entry prior to editing
# %S = the text string being inserted or deleted, if any
# %v = the type of validation that is currently set
# %V = the type of validation that triggered the callback
# (key, focusin, focusout, forced)
# %W = the tk name of the widget
vcmd = (self.register(self.onValidate),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
self.entry = tk.Entry(self, validate="key", validatecommand=vcmd)
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 onValidate(self, d, i, P, s, S, v, V, W):
self.text.delete("1.0", "end")
self.text.insert("end","OnValidate:\n")
self.text.insert("end","d='%s'\n" % d)
self.text.insert("end","i='%s'\n" % i)
self.text.insert("end","P='%s'\n" % P)
self.text.insert("end","s='%s'\n" % s)
self.text.insert("end","S='%s'\n" % S)
self.text.insert("end","v='%s'\n" % v)
self.text.insert("end","V='%s'\n" % V)
self.text.insert("end","W='%s'\n" % W)
# Disallow anything but lowercase letters
if S == S.lower():
return True
else:
self.bell()
return False
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True)
root.mainloop()
有关调用register
方法时幕后发生的事情的更多信息,请参阅为什么 tkinter 输入验证需要调用 register()?
有关规范文档,请参阅Tcl/Tk 条目手册页的验证部分
在研究和试验了 Bryan 的代码之后,我制作了一个最小版本的输入验证。 以下代码将放置一个输入框,并且只接受数字。
from tkinter import *
root = Tk()
def testVal(inStr,acttyp):
if acttyp == '1': #insert
if not inStr.isdigit():
return False
return True
entry = Entry(root, validate="key")
entry['validatecommand'] = (entry.register(testVal),'%P','%d')
entry.pack()
root.mainloop()
也许我应该补充一点,我仍在学习 Python,我很乐意接受任何和所有评论/建议。
使用Tkinter.StringVar
来跟踪Entry
小部件的值。 您可以通过在其上设置trace
来验证StringVar
的值。
这是一个简短的工作程序,它只接受Entry
小部件中的有效浮点数。
try:
from tkinter import *
except ImportError:
from Tkinter import * # Python 2
root = Tk()
sv = StringVar()
def validate_float(var):
new_value = var.get()
try:
new_value == '' or float(new_value)
validate_float.old_value = new_value
except:
var.set(validate_float.old_value)
validate_float.old_value = '' # Define function attribute.
# trace wants a callback with nearly useless parameters, fixing with lambda.
sv.trace('w', lambda nm, idx, mode, var=sv: validate_float(var))
ent = Entry(root, textvariable=sv)
ent.pack()
ent.focus_set()
root.mainloop()
Bryan 的回答是正确的,但是没有人提到 tkinter 小部件的“invalidcommand”属性。
一个很好的解释在这里:http: //infohost.nmt.edu/tcc/help/pubs/tkinter/web/entry-validation.html
在链接断开的情况下复制/粘贴文本
Entry 小部件还支持一个 invalidcommand 选项,该选项指定一个回调函数,只要 validatecommand 返回 False,就会调用该回调函数。 此命令可以通过使用 .set() 方法在小部件的关联文本变量上修改小部件中的文本。 设置此选项的工作方式与设置 validatecommand 相同。 你必须使用 .register() 方法来包装你的 Python 函数; 此方法将包装函数的名称作为字符串返回。 然后,您将传递该字符串或作为包含替换代码的元组的第一个元素作为 invalidcommand 选项的值。
注意:只有一件事我不知道该怎么做:如果您向条目添加验证,并且用户选择部分文本并键入新值,则无法捕获原始值并重置入口。 这是一个例子
定义一个返回布尔值的函数,该布尔值指示输入是否有效。
将其注册为 Tcl 回调,并将回调名称作为validatecommand
传递给小部件。
例如:
import tkinter as tk
def validator(P):
"""Validates the input.
Args:
P (int): the value the text would have after the change.
Returns:
bool: True if the input is digit-only or empty, and False otherwise.
"""
return P.isdigit() or P == ""
root = tk.Tk()
entry = tk.Entry(root)
entry.configure(
validate="key",
validatecommand=(
root.register(validator),
"%P",
),
)
entry.grid()
root.mainloop()
参考。
在研究Bryan Oakley 的回答时,一些事情告诉我可以开发出更通用的解决方案。 以下示例介绍了用于验证目的的模式枚举、类型字典和设置函数。 请参见第 48 行的示例用法和其简单性的演示。
#! /usr/bin/env python3
# https://stackoverflow.com/questions/4140437
import enum
import inspect
import tkinter
from tkinter.constants import *
Mode = enum.Enum('Mode', 'none key focus focusin focusout all')
CAST = dict(d=int, i=int, P=str, s=str, S=str,
v=Mode.__getitem__, V=Mode.__getitem__, W=str)
def on_validate(widget, mode, validator):
# http://www.tcl.tk/man/tcl/TkCmd/ttk_entry.htm#M39
if mode not in Mode:
raise ValueError('mode not recognized')
parameters = inspect.signature(validator).parameters
if not set(parameters).issubset(CAST):
raise ValueError('validator arguments not recognized')
casts = tuple(map(CAST.__getitem__, parameters))
widget.configure(validate=mode.name, validatecommand=[widget.register(
lambda *args: bool(validator(*(cast(arg) for cast, arg in zip(
casts, args)))))]+['%' + parameter for parameter in parameters])
class Example(tkinter.Frame):
@classmethod
def main(cls):
tkinter.NoDefaultRoot()
root = tkinter.Tk()
root.title('Validation Example')
cls(root).grid(sticky=NSEW)
root.grid_rowconfigure(0, weight=1)
root.grid_columnconfigure(0, weight=1)
root.mainloop()
def __init__(self, master, **kw):
super().__init__(master, **kw)
self.entry = tkinter.Entry(self)
self.text = tkinter.Text(self, height=15, width=50,
wrap=WORD, state=DISABLED)
self.entry.grid(row=0, column=0, sticky=NSEW)
self.text.grid(row=1, column=0, sticky=NSEW)
self.grid_rowconfigure(1, weight=1)
self.grid_columnconfigure(0, weight=1)
on_validate(self.entry, Mode.key, self.validator)
def validator(self, d, i, P, s, S, v, V, W):
self.text['state'] = NORMAL
self.text.delete(1.0, END)
self.text.insert(END, 'd = {!r}\ni = {!r}\nP = {!r}\ns = {!r}\n'
'S = {!r}\nv = {!r}\nV = {!r}\nW = {!r}'
.format(d, i, P, s, S, v, V, W))
self.text['state'] = DISABLED
return not S.isupper()
if __name__ == '__main__':
Example.main()
import tkinter
tk=tkinter.Tk()
def only_numeric_input(e):
#this is allowing all numeric input
if e.isdigit():
return True
#this will allow backspace to work
elif e=="":
return True
else:
return False
#this will make the entry widget on root window
e1=tkinter.Entry(tk)
#arranging entry widget on screen
e1.grid(row=0,column=0)
c=tk.register(only_numeric_input)
e1.configure(validate="key",validatecommand=(c,'%P'))
tk.mainloop()
#very usefull for making app like calci
如果您只想设置数字和最大字符,此代码会有所帮助。
from tkinter import *
root = Tk()
def validate(P):
if len(P) == 0 or len(P) <= 10 and P.isdigit(): # 10 characters
return True
else:
return False
ent = Entry(root, validate="key", validatecommand=(root.register(validate), '%P'))
ent.pack()
root.mainloop()
这是@Steven Rumbalski 通过跟踪对StringVar
的更改来验证Entry
小部件值的答案的改进版本——我已经通过就地编辑它进行了调试和一定程度的改进。
下面的版本将所有内容放入一个StringVar
子类中,以更好地封装正在发生的事情,更重要的是允许它的多个独立实例同时存在而不会相互干扰——他的实现存在潜在问题,因为它利用函数属性而不是实例属性,它们本质上与全局变量相同,在这种情况下可能会导致问题。
try:
from tkinter import *
except ImportError:
from Tkinter import * # Python 2
class ValidateFloatVar(StringVar):
"""StringVar subclass that only allows valid float values to be put in it."""
def __init__(self, master=None, value=None, name=None):
StringVar.__init__(self, master, value, name)
self._old_value = self.get()
self.trace('w', self._validate)
def _validate(self, *_):
new_value = self.get()
try:
new_value == '' or float(new_value)
self._old_value = new_value
except ValueError:
StringVar.set(self, self._old_value)
root = Tk()
ent = Entry(root, textvariable=ValidateFloatVar(value=42.0))
ent.pack()
ent.focus_set()
ent.icursor(END)
root.mainloop()
回应orionrobert 处理通过选择替换文本而不是单独删除或插入的简单验证的问题:
所选文本的替换被处理为删除后插入。 这可能会导致问题,例如,删除应将光标移至左侧,而替换应将光标移至右侧。 幸运的是,这两个过程是一个接一个地执行的。 因此,我们可以区分删除本身和由于替换而直接后跟插入的删除,因为后者不会更改删除和插入之间的空闲标志。
这是使用替代标志和Widget.after_idle()
来利用的。 after_idle()
在事件队列的末尾执行 lambda 函数:
class ValidatedEntry(Entry):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.tclValidate = (self.register(self.validate), '%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
# attach the registered validation function to this spinbox
self.config(validate = "all", validatecommand = self.tclValidate)
def validate(self, type, index, result, prior, indelText, currentValidationMode, reason, widgetName):
if typeOfAction == "0":
# set a flag that can be checked by the insertion validation for being part of the substitution
self.substitutionFlag = True
# store desired data
self.priorBeforeDeletion = prior
self.indexBeforeDeletion = index
# reset the flag after idle
self.after_idle(lambda: setattr(self, "substitutionFlag", False))
# normal deletion validation
pass
elif typeOfAction == "1":
# if this is a substitution, everything is shifted left by a deletion, so undo this by using the previous prior
if self.substitutionFlag:
# restore desired data to what it was during validation of the deletion
prior = self.priorBeforeDeletion
index = self.indexBeforeDeletion
# optional (often not required) additional behavior upon substitution
pass
else:
# normal insertion validation
pass
return True
当然,在替换之后,在验证删除部分的同时,仍然不知道是否会出现插入。 然而幸运的是,通过.set()
、 .icursor()
、 .index(SEL_FIRST)
、 .index(SEL_LAST)
、 .index(INSERT)
,我们可以回顾性地实现最想要的行为(因为我们的新替代标记与插入是一个新的、独特的、最终的事件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.