经典小部件和 ttk 小部件之间的条目小部件验证区别
Entry widget validation difference between classic and ttk widgets
我发现 Python 3.5 中经典小部件和 ttk 小部件之间的 Entry 小部件验证存在意外差异。
使用经典小部件:
from tkinter import *
def validate(reason):
print("--> validate:", reason)
return(True)
def change():
var.set("data")
root = Tk()
vc = root.register(validate)
var = StringVar()
Entry(root, textvariable = var, validate = "all", validatecommand = (vc, "%V")).pack()
Button(root, text = "Change", command = change).pack()
root.mainloop()
使用 ttk 小部件:
from tkinter import *
from tkinter.ttk import *
... same code as above
对于经典小部件,当按下 "Change" 按钮时,将调用验证函数,原因 == "forced",这似乎符合 Tk 文档。对于 ttk 小部件,当按下 "Change" 按钮时,不会调用验证函数。否则,验证函数似乎对两种情况都具有相同的行为。有人知道这是错误还是功能?
这是一项功能。根据 official ttk documentation:
DIFFERENCES FROM TK ENTRY WIDGET VALIDATION
The standard Tk entry widget automatically disables validation (by
setting -validate to none) if the -validatecommand or -invalidcommand
modifies the entry's value. The Tk themed entry widget only disables
validation if one of the validation scripts raises an error, or if
-validatecommand does not return a valid boolean value. (Thus, it is not necessary to re-enable validation after modifying the entry value
in a validation script).
In addition, the standard entry widget invokes validation whenever the
linked -textvariable is modified; the Tk themed entry widget does not.
我发现 Python 3.5 中经典小部件和 ttk 小部件之间的 Entry 小部件验证存在意外差异。
使用经典小部件:
from tkinter import *
def validate(reason):
print("--> validate:", reason)
return(True)
def change():
var.set("data")
root = Tk()
vc = root.register(validate)
var = StringVar()
Entry(root, textvariable = var, validate = "all", validatecommand = (vc, "%V")).pack()
Button(root, text = "Change", command = change).pack()
root.mainloop()
使用 ttk 小部件:
from tkinter import *
from tkinter.ttk import *
... same code as above
对于经典小部件,当按下 "Change" 按钮时,将调用验证函数,原因 == "forced",这似乎符合 Tk 文档。对于 ttk 小部件,当按下 "Change" 按钮时,不会调用验证函数。否则,验证函数似乎对两种情况都具有相同的行为。有人知道这是错误还是功能?
这是一项功能。根据 official ttk documentation:
DIFFERENCES FROM TK ENTRY WIDGET VALIDATION
The standard Tk entry widget automatically disables validation (by setting -validate to none) if the -validatecommand or -invalidcommand modifies the entry's value. The Tk themed entry widget only disables validation if one of the validation scripts raises an error, or if -validatecommand does not return a valid boolean value. (Thus, it is not necessary to re-enable validation after modifying the entry value in a validation script).
In addition, the standard entry widget invokes validation whenever the linked -textvariable is modified; the Tk themed entry widget does not.