classic和ttk小部件之间的入口小部件验证差异

2024-04-26 23:42:51 发布

您现在位置:Python中文网/ 问答频道 /正文

在python3.5中,我发现了classic和ttk小部件在条目小部件验证方面的意外差异。在

使用经典小工具:

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小部件:

^{pr2}$

对于经典的widgets,当按下“Change”按钮时,validate函数会被reason==“forced”调用,这似乎符合Tk doc。对于ttk小部件,当按下“Change”按钮时,不会调用validate函数。否则,validate函数在这两种情况下似乎都具有相同的行为。有人知道这是一个bug还是一个特性?在


Tags: 函数部件vardefrootchangevalidate按钮
1条回答
网友
1楼 · 发布于 2024-04-26 23:42:51

这是一个特色。根据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.

相关问题 更多 >