简体   繁体   中英

How to bind again after unbinding a functiion in tkinter?

I am trying to bind a button to a different function after the user selects some number of points on the image with another function. Currently, I am doing like this-

canvas.bind("<Button-1>", func1)
canvas.unbind("<Button-1>")
canvas.bind("<Button-1>", func2)

This does not solve my problem as only func2 is executing. I have also tried adding the unbind statement to func1 and func2 (which runs after some condition is satisfied) but same thing is happening. Is there a way to make this run sequentially so that I can bind with func1 -> unbind func1 -> bind with func2 -> unbind func2.

I want to do the point selection in one image only without putting the image again on canvas. Also, is there any better way to approach this?

Take a look at this example, where once you click twice on the root window, then func2 will be used.

from tkinter import *

root = Tk()

count = 0
def func1(event):
    global count
    count += 1
    print('FUNC1')
    
    if count == 2:
        root.unbind('<Button-1>')
        root.bind('<Button-1>',func2)
        count = 0

def func2(event):
    print('FUNC2') #you can create similar logic to switch back to func1 here too 

root.bind('<Button-1>',func1)

root.mainloop()

Using a counter is a easy way to do this, not sure if this is what your looking for, but you can get an idea from here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM