Python

Python,第1张

Python

正确的答案是,使用

validatecommand
部件的属性。不幸的是,尽管在
Tkinter
中有足够的文档记录,但该功能在
Tkinter
领域中的记录不足。即使没有很好地记录下来,它也具有验证所需的一切,而无需求助于绑定或跟踪变量,或在验证过程中修改小部件。

诀窍是要知道您可以让

Tkinter
将特殊值传递给
validate
命令。这些值为您提供了决定数据是否有效所需的所有信息:编辑之前的值,编辑之后的值(如果编辑有效)以及其他几位信息。但是,要使用这些命令,您需要做一点巫术,以使此信息传递到您的
validate
命令。

注意:验证命令返回

True
或至关重要
False
。任何其他情况都会导致该小部件的验证被关闭。

这是一个仅允许小写的示例(并显示所有这些时髦的值):

import tkinter as tk  # python 3.x# import Tkinter as tk # python 2.xclass 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 Falseif __name__ == "__main__":    root = tk.Tk()    Example(root).pack(fill="both", expand=True)    root.mainloop()

有关调用该

register
方法时幕后情况的更多信息,请参见输入验证
tkinter



欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/4947985.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-13
下一篇2022-11-13

发表评论

登录后才能评论

评论列表(0条)

    保存