2017-09-18 8 views
0

私はこのコードを見つけました: 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)を使用する必要がありますが、それは文字列です。私はregisterメソッドが唯一の文字列としてパスやウィジェットのポインタを与えるのでvalitation機能を付属しているウィジェットを変更する方法が必要

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

:私はこのようなものが必要。しかし、私はウィジェットを修正する必要があります。

私はすべてのウィジェットを "instance"という名前のリストに入れて、インスタンス内でfor wを作成し、すべてのウィジェットを%Wのバリデータ関数と比較して、同じウィジェット名を見つけるまで、リスト内のオブジェクトを変更します。

これを行う方法はありますか?君たちありがとう!

答えて

1

検証機能は、検証中にウィジェットの内容を変更するためのものではありません。 canonical tcl/tk documentationから

:あなたはvalidateCommandまたはinvalidCommandのいずれかの中からエントリウィジェットを編集するとき

検証オプションもなしに自分自身を設定します。そのようなエディションは、検証されていたものを上書きします。検証中に入力ウィジェットを編集したい場合(たとえば、それを{}に設定する)、まだ検証オプションセットがある場合は、コマンドを含める必要があります。

言い換えれば、任意のウィジェットから呼び出すことができるnametowidgetメソッドを使用して、ウィジェット名をウィジェット参照に追加します。

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

ありがとうございました! –

関連する問題