1
from Tkinter import *
root=Tk()
e1=Entry(root)
e1.pack()
e2=Entry(root)
e2.pack()


def test(x):
    if x==1:            # if event on entry e1 
        print 'e1 event' # do some thing
    elif x==2:            # also if event on entry e2    
        print 'e2 event'  # do some thing else
    else: print 'no event' 

root.bind_class("Entry","<FocusOut>",test(2)) #this can be some thing you suggest to me
root.bind_class("Entry","<FocusOut>",test(1)) # can be some thing you suggest to me   


root.mainloop()

コメントで物事を行う方法は?エントリに入力するカーソルがあり、タブキーでフォーカスアウトすると、他のエントリとは異なることができるプログラムが必要です。

4

1 に答える 1

3
from Tkinter import *

#----------------------------------------------------------------------

def test(x):    
    #print "I'm in event:", x
    if x == 1:            # if event on entry e1 
        print 'e1 event' # do some thing
    elif x == 2:            # also if event on entry e2    
        print 'e2 event'  # do some thing else
    else: 
        print 'no event' 

def test1(x):
    test(1)

def test2(x):
    test(2)

#----------------------------------------------------------------------

root=Tk()

e1=Entry(root)
e1.pack()

e2=Entry(root)
e2.pack()

root.bind_class(e1, "<FocusOut>", test1) #this can be some thing you suggest to me
root.bind_class(e2, "<FocusOut>", test2) # can be some thing you suggest to me   

root.mainloop()

編集:

この方法でもバインドできます。

e1.bind("<FocusOut>", test1)
e2.bind("<FocusOut>", test2)

ただし、常に引数なしで関数名のみを指定してください。

関数は、イベント オブジェクトを取得するために 1 つの引数を取得する必要があります

def test1(x):
    print x # <Tkinter.Event instance at 0xb74a6fcc>
    test(1)

編集:

オブジェクト指向プログラミングの学習を開始して、「よりクリーンな」プログラム、つまりより構造化されたプログラムを作成します。

from Tkinter import *

#----------------------------------------------------------------------

class MainWindow():

    def __init__(self, root):
        self.e1 = Entry(root)
        self.e1.pack()

        self.e2 = Entry(root)
        self.e2.pack()

        self.e1.bind("<FocusOut>", self.test1)
        self.e2.bind("<FocusOut>", self.test2)

    #-------------------

    def test(self, x):
        if x == 1:           
            print 'e1 event'
        elif x == 2:
            print 'e2 event'
        else:
            print 'no event' 

    #-------------------

    def test1(self, x):
        print x
        self.test(1)

    #-------------------

    def test2(self, x):
        self.test(2)

#----------------------------------------------------------------------

root = Tk()
MainWindow(root)
root.mainloop()

編集:

最終バージョン - イベント オブジェクトを使用する 1 つの関数を使用

from Tkinter import *

#----------------------------------------------------------------------

class MainWindow():

    def __init__(self, root):
        self.e1 = Entry(root)
        self.e1.pack()

        self.e2 = Entry(root)
        self.e2.pack()

        self.e1.bind("<FocusOut>", self.test)
        self.e2.bind("<FocusOut>", self.test)
        # or
        # root.bind_class("Entry", "<FocusOut>", self.test)

    #-------------------

    def test(self, event):
        if event.widget == self.e1:
            #print "e1:", event.widget.get()
            print "e1:", self.e1.get()

        if event.widget == self.e2:
            #print "e2:", event.widget.get()
            print "e2:", self.e2.get()

#----------------------------------------------------------------------

root = Tk()
MainWindow(root)
root.mainloop()

イベントの詳細: Tkinter イベントとバインディング

于 2013-11-03T21:25:16.580 に答える