如何使用验证器功能中的部件名称?

问题描述:

我发现这个代码: Interactively validating Entry widget content in tkinter如何使用验证器功能中的部件名称?

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() 

我需要使用实例(%W),但它是一个字符串。我需要的是这样的:

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) 
     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): 
     print type(W) #This gives <string> 
     W = instance(W) #Something to convert the string to an instance like int() 
     W.get() 
     W.insert('Some text',0) 

if __name__ == "__main__": 
    root = tk.Tk() 
    Example(root).pack(fill="both", expand=True) 
    root.mainloop() 

我需要一种方法来修改贴有valitation功能小部件,因为该注册方法只给出了路径或部件作为一个字符串的指针。但是我需要修改这个小部件。

我一直在考虑把所有小部件放在一个名为“instance”的列表中,然后在“实例中”为“w”,并将所有小部件与验证器函数的%W进行比较,直到找到相同的小部件名称,然后修改列表中的objet。

有没有办法做到这一点更容易?感谢你们!

验证功能不适合您在验证期间修改小部件的内容。

canonical tcl/tk documentation

Validate选项也将自身设置为无,当你无论从validateCommand或invalidCommand内编辑的条目小部件。这些版本将覆盖正在验证的版本。如果您希望编辑录入组件验证过程中(例如将其设置为{}),并且仍然有验证选项设置,则应该包括命令

话虽这么说,如果你需要,你可以转换使用方法nametowidget可以通过任何窗口小部件调用窗口小部件名称。

def onValidate(self, d, i, P, s, S, v, V, W): 
    widget = self.nametowidget(W) 
    ... 
+0

非常感谢! –