Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/339.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何通过单击运行一系列事件_Python_Python 3.x_Tkinter - Fatal编程技术网

Python 如何通过单击运行一系列事件

Python 如何通过单击运行一系列事件,python,python-3.x,tkinter,Python,Python 3.x,Tkinter,我希望程序通过单击来运行不同的功能。我不想要按钮,只想让它运行与左键点击。 在下面的代码中,它通过左键单击运行getorigin,我不知道如何让它通过下一次左键单击运行other_函数,然后通过再次左键单击运行third_函数 from tkinter import * # --- functions --- def getorigin(event): x0 = event.x y0 = event.y print('getorigin:', x0, y0) def

我希望程序通过单击来运行不同的功能。我不想要按钮,只想让它运行与左键点击。 在下面的代码中,它通过左键单击运行
getorigin
,我不知道如何让它通过下一次左键单击运行
other_函数
,然后通过再次左键单击运行
third_函数

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()
从tkinter导入*
#---功能---
def getorigin(事件):
x0=事件x
y0=事件。y
打印('getorigin:',x0,y0)
def其他_功能(事件):
打印(“其他功能”,x0+1,y0+1)
def第三功能(事件):
打印('第三功能',x0+1,y0+1)
#---梅因---
#创建全局变量
x0=0
y0=0
root=Tk()
w=画布(根,宽度=1000,高度=640)
w、 包()
w、 绑定(“,getorigin)
root.mainloop()

您可以将左键单击与计算单击次数的函数绑定,并在此基础上运行函数

def userClicked(event):
    global clickTimes
    clickTimes += 1

    if clickTimes == 1:
        getorigin(event)
    elif clickTimes == 2:
        other_function(event)
    elif clickTimes == 3:
        third_function(event)

您需要在主菜单中将全局
clickTimes
声明为0

您可以将函数放入一个列表中,然后在每次处理单击时旋转该列表

创建函数后,将其添加到列表中:

def getorigin(event):
    ...
def other_function(event):
    ...
def third_function(event):
    ...

functions = [getorigin, other_function, third_function]
接下来,将一个函数与一个按钮单击相关联,该按钮单击会弹出列表中的第一个函数,将其移动到末尾,然后执行该函数:

def handle_click(event):
    global functions
    func = functions.pop(0)
    functions.append(func)
    func(event)

w.bind("<Button-1>", handle_click)
def handle_单击(事件):
全局函数
func=functions.pop(0)
functions.append(func)
职能(活动)
w、 绑定(“,句柄\单击)