python - 如何做点击运行的一系列事件

标签 python python-3.x tkinter

我想让程序通过点击运行不同的功能。我不想要按钮,只希望它通过左键单击运行。 在下面的代码中,它通过左键单击运行 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/

相关文章:

python 为什么在类中定义的一个函数不能在同一类中的另一个函数中调用?

Python Tkinter 浏览文件 - 问题

python - py2exe 与应用程序的不同部分

python - 抓取的 Span 返回 None Get_Text() Python Beautiful Soup

python - 通过循环 train_test_split 和不循环训练来训练模型

python - Python 的 super() 如何处理多重继承?

Python字典: If the keys are the same multiply the values

python - IP摄像机Python错误

python-3.x - 如何使用 tkinter 按钮执行 python 文件?

python - 标准输出到 tkinter GUI