在python中删除和更改tkinter事件绑定 [英] Deleting and changing a tkinter event binding in python

查看:1257
本文介绍了在python中删除和更改tkinter事件绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何阻止某个事件被处理或切换所调用的功能?



修改后的代码

 从Tkinter导入* 

类GUI:
def __init __(self,root):
Window = Frame(root)
self.DrawArea = Canvas(Window)
self.DrawArea.pack()
Window.pack()

self.DrawArea .bind(< Button 1>,self.starttracking)

def updatetracking(self,event):
print event.x,event.y

def finishtracking(self,event):
self.DrawArea.bind(< Button 1>,self.starttracking)
self.DrawArea.unbind(< Motion>)

def starttracking(self,event):
print event.x,event.y
self.DrawArea.bind(< Motion>,self.updatetracking)
self.DrawArea.bind(< Button 1>,self.finishtracking)



如果__name__ =='__main__':
roo t = Tk()
App = GUI(root)
root.mainloop()


解决方案

您可以使用事件的新功能再次调用 bind()。由于您没有使用第三个参数,添加,在 bind()中,这将覆盖任何已经那里。默认情况下,该参数是'',但它也接受+,这将为回调添加一个回调如果您开始使用该可选参数,则需要使用 unbind()功能来删除单个回调。当您调用 bind()时,将返回一个 funcid 。您可以将 funcid 作为第二个参数传递给 unbind()



示例:

  self.btn_funcid = self.DrawArea.bind(< Button 1>,self .my_button_callback,+)

#然后一段时间后,只需要删除'my_button_callback':
self.DrawArea.unbind(< Button 1>,self.btn_funcid )

#但是如果要删除事件的所有回调:
self.DrawArea.unbind(< Button 1>)


How do i stop an event from being processed or switch what function is called for it?

Revised Code:

from Tkinter import *

class GUI:
    def __init__(self,root):
        Window = Frame(root)
        self.DrawArea = Canvas(Window)
        self.DrawArea.pack()
        Window.pack()

        self.DrawArea.bind("<Button 1>",self.starttracking)

    def updatetracking(self,event):
        print event.x,event.y

    def finishtracking(self,event):
        self.DrawArea.bind("<Button 1>",self.starttracking)
        self.DrawArea.unbind("<Motion>")

    def starttracking(self,event):
        print event.x,event.y
        self.DrawArea.bind("<Motion>",self.updatetracking)
        self.DrawArea.bind("<Button 1>",self.finishtracking)



if __name__ == '__main__':
    root = Tk()
    App = GUI(root)
    root.mainloop()

解决方案

You can simply just call bind() again with the new function for the event. Since you are not making use of the third parameter, add, in bind() this will just overwrite whatever is already there. By default this parameter is '' but it also accepts "+", which will add a callback to the callbacks already triggered by that event.

If you start using that optional argument however you will need to use the unbind() function to remove individual callbacks. When you call bind() a funcid is returned. You can pass this funcid as the second parameter to unbind().

Example:

self.btn_funcid = self.DrawArea.bind("<Button 1>", self.my_button_callback, "+")

# Then some time later, to remove just the 'my_button_callback':
self.DrawArea.unbind("<Button 1>", self.btn_funcid)

# But if you want to remove all of the callbacks for the event:
self.DrawArea.unbind("<Button 1>")

这篇关于在python中删除和更改tkinter事件绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆