2017-09-18 1 views
0

Je trouve ce code: Interactively validating Entry widget content in tkinterComment utiliser un nom de widget depuis une fonction de validation?

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

je dois utiliser l'instance (% W), mais il est une chaîne. Je besoin de quelque chose comme ceci:

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

je besoin d'un moyen de modifier le widget qui a joint la fonction valitation parce que la méthode de registre ne donne que le chemin ou le pointeur du widget comme une chaîne. Mais j'ai besoin de modifier le widget. J'ai pensé à placer tous les widgets dans une liste nommée "instance", puis faire un "for w dans instances" et comparer tous les widgets avec% W de la fonction validator, jusqu'à ce qu'il trouve le même nom de widget, puis modifiez l'objet dans la liste.

Est-il possible de le faire plus facile? Merci les gars!

Répondre

1

La fonction de validation n'a pas été conçue pour vous de modifier le contenu du widget lors de la validation.

De l'canonical tcl/tk documentation:

L'option validate se fixera également aucun lorsque vous modifiez le widget d'entrée à l'intérieur soit validateCommand ou invalidCommand. Ces éditions remplaceront celle qui était en cours de validation. Si vous souhaitez modifier le widget d'entrée (par exemple le mettre à {}) lors de la validation et ont encore l'ensemble des options de validation, vous devez inclure la commande

Cela étant dit, si vous avez besoin, vous pouvez convertir le nom du widget à une référence de widget en utilisant la méthode nametowidget que vous pouvez appeler via n'importe quel widget.

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

Merci beaucoup! –