小编典典

Python -在Tkinter中交互式验证Entry小部件内容

python

交互验证tkinter Entry小部件中的内容的推荐技术是什么?

我已经阅读了有关使用validate=True和的文章validatecommand=command,并且看来这些功能受到以下事实的限制:如果validatecommand命令更新了Entry小部件的值,这些功能将被清除。

鉴于这种行为,我们应该绑定的KeyPress,Cut以及Paste事件和监视/更新我们的Entry小部件的价值,通过这件事情?(还有我可能错过的其他相关事件?)

还是我们应该完全忘记交互式验证,而只对FocusOut事件进行验证?


阅读 975

收藏
2020-02-08

共1个答案

小编典典

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

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

2020-02-08