我想让程序通过点击运行不同的功能。我不想要按钮,只希望它通过左键单击运行。
在下面的代码中,它通过左键单击运行 getorigin
,我不知道如何让它通过下一次左键运行 other_function
,然后运行 third_function
再单击一次左键。
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()
最佳答案
您可以将左键单击与计算点击次数并基于此运行函数的函数绑定(bind)。
def userClicked(event):
global clickTimes
clickTimes += 1
if clickTimes == 1:
getorigin(event)
elif clickTimes == 2:
other_function(event)
elif clickTimes == 3:
third_function(event)
您需要在您的 main 中将全局 clickTimes
声明为 0
关于python - 如何做点击运行的一系列事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56027316/