Python 等待第二次鼠标单击画布
我正在使用Tkinter和Python,我想单击画布上的两个位置,然后在这两个点之间画一条线Python 等待第二次鼠标单击画布,python,tkinter,tkinter-canvas,Python,Tkinter,Tkinter Canvas,我正在使用Tkinter和Python,我想单击画布上的两个位置,然后在这两个点之间画一条线 使用.bind()方法,我可以轻松调用一个函数,该函数获取光标的位置并设置第一个点,但我不知道如何使该函数等待第二次鼠标单击。当然有解决办法,但我想知道是否可以让函数无限期地等待到第二次单击。无需等待。第一次单击设置标志,第二次单击检查标志 class Example(...): def __init__(...): self.first_coord = None
使用.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:
,对吗?对我来说很清楚,但为了更好地阅读/完全正确的代码,这需要一次更改。