Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.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_Tkinter_Tkinter Canvas - Fatal编程技术网

Python 等待第二次鼠标单击画布

Python 等待第二次鼠标单击画布,python,tkinter,tkinter-canvas,Python,Tkinter,Tkinter Canvas,我正在使用Tkinter和Python,我想单击画布上的两个位置,然后在这两个点之间画一条线 使用.bind()方法,我可以轻松调用一个函数,该函数获取光标的位置并设置第一个点,但我不知道如何使该函数等待第二次鼠标单击。当然有解决办法,但我想知道是否可以让函数无限期地等待到第二次单击。无需等待。第一次单击设置标志,第二次单击检查标志 class Example(...): def __init__(...): self.first_coord = None

我正在使用Tkinter和Python,我想单击画布上的两个位置,然后在这两个点之间画一条线


使用.bind()方法,我可以轻松调用一个函数,该函数获取光标的位置并设置第一个点,但我不知道如何使该函数等待第二次鼠标单击。当然有解决办法,但我想知道是否可以让函数无限期地等待到第二次单击。

无需等待。第一次单击设置标志,第二次单击检查标志

class Example(...):
    def __init__(...):
        self.first_coord = None
        ...
        canvas.bind("<1>", self.on_click)
    def on_click(self, event):
        if self.first_coord is None:
            self.first_coord = (event.x, event.y)
        else:
            second_coord = (event.x, event.y)
            <draw the line here>
            self.first_coord = None
类示例(…):
定义初始化(…):
self.first_coord=无
...
canvas.bind(“,self.on_单击)
单击时的def(自身,事件):
如果self.first_coord为无:
self.first_coord=(event.x,event.y)
其他:
第二个坐标=(event.x,event.y)
self.first_coord=无

无需等待。第一次单击设置标志,第二次单击检查标志

class Example(...):
    def __init__(...):
        self.first_coord = None
        ...
        canvas.bind("<1>", self.on_click)
    def on_click(self, event):
        if self.first_coord is None:
            self.first_coord = (event.x, event.y)
        else:
            second_coord = (event.x, event.y)
            <draw the line here>
            self.first_coord = None
类示例(…):
定义初始化(…):
self.first_coord=无
...
canvas.bind(“,self.on_单击)
单击时的def(自身,事件):
如果self.first_coord为无:
self.first_coord=(event.x,event.y)
其他:
第二个坐标=(event.x,event.y)
self.first_coord=无

@Bryan:应该是
否则:
,而不是
elee:
,对吗?对我来说很清楚,但为了更好地阅读/完全正确的代码,这需要做一个更改。@Bryan:应该是
其他:
,而不是
elee:
,对吗?对我来说很清楚,但为了更好地阅读/完全正确的代码,这需要一次更改。