Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do a series of events run by clicks

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()
like image 873
Pei Li Avatar asked Dec 07 '25 04:12

Pei Li


1 Answers

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

like image 138
Matt M Avatar answered Dec 08 '25 18:12

Matt M



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!