如何处理每个条目的事件

时间:2013-11-03 20:39:44

标签: python tkinter

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键进行聚焦时,可以做一些与其他条目不同的事情。

1 个答案:

答案 0 :(得分: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