Reputation: 53
I want the program to run different functions by click. I don't want buttons, just want it to run with left clicks.
In the code below, it runs getorigin
by a left click, I don't know how to make it run other_function
by next left click, and then run third_function
by one more left click.
from tkinter import *
# --- functions ---
def getorigin(event):
x0 = event.x
y0 = event.y
print('getorigin:', x0, y0)
def other_function(event):
print('other function', x0+1, y0+1)
def third_function(event):
print('third function', x0+1, y0+1)
# --- main ---
# create global variables
x0 = 0
y0 = 0
root = Tk()
w = Canvas(root, width=1000, height=640)
w.pack()
w.bind("<Button-1>", getorigin)
root.mainloop()
Upvotes: 3
Views: 72
Reputation: 385830
You could put the functions in a list, and then rotate the list each time you process a click.
Sometime after you've created the functions, add them to a list:
def getorigin(event):
...
def other_function(event):
...
def third_function(event):
...
functions = [getorigin, other_function, third_function]
Next, associate a function with a button click that pops the first function off of the list, moves it to the end, and then executes it:
def handle_click(event):
global functions
func = functions.pop(0)
functions.append(func)
func(event)
w.bind("<Button-1>", handle_click)
Upvotes: 0
Reputation: 710
You could bind the left-click with a function that counts clicks and runs functions based on that.
def userClicked(event):
global clickTimes
clickTimes += 1
if clickTimes == 1:
getorigin(event)
elif clickTimes == 2:
other_function(event)
elif clickTimes == 3:
third_function(event)
You would need to declare that global clickTimes
as 0 down in your main
Upvotes: 1