2013-11-03 31 views
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() 

如何處理評論?我希望程序能夠將光標鍵入到哪個條目中,並且在使用Tab鍵進行聚焦時,可以執行與其他條目不同的操作。如何處理每個單獨條目上的事件

回答

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

編輯:

最新版本 - 使用事件對象一個功能

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 Events and Bindings

+1

完美answer..thanks –

相關問題