首页 文章

生成事件时超出了tkinter最大递归深度

提问于
浏览
1

我试图用一些方法绑定鼠标运动(按下/未按下) . 我试图处理鼠标动作,而鼠标按钮用''按下,另一个按'' . 我发现当我只有..bind('',somemethod1)时,无论按下鼠标按钮都会调用somemethod1,但是当我也有..bind('',somemethod2)时,按下鼠标按钮时不调用somemethod1 . 添加'add ='''似乎不起作用 .

def bind_mouse(self):
    self.canvas.bind('<Button1-Motion>', self.on_button1_motion1)
    self.canvas.bind('<Motion>', self.on_mouse_unpressed_motion1)

def on_button1_motion1(self, event):
    print(self.on_button1_motion1.__name__)

def on_mouse_unpressed_motion1(self, event):
    print(self.on_mouse_unpressed_motion1.__name__)

所以我改为修改on_button1_motion1方法,如下所示:

def on_button1_motion1(self, event):
    print(self.on_button1_motion1.__name__)
    self.canvas.event_generate('<Motion>')

但是当我尝试这个时,我得到了这个运行时错误:

回溯(最近一次调用最后一次):文件“D:/ save / WORKSHOP / py / tkinter / Blueprints / Pycrosoft Paintk / view.py”,第107行,在root.mainloop()文件“C:\ Users \ smj \ AppData \ local \ Programs \ Python \ Python35 \ lib \ tkinter__init __ . py“,第1131行,在mainloop中self.tk.mainloop(n)RecursionError:超出最大递归深度

任何人都可以向我解释为什么会这样吗?我知道我可以通过在on_button1_motion1方法中调用on_mouse_unpressed_motion1方法而不是生成事件来解决这个问题,但我想知道为什么其他方法不起作用 . 谢谢

1 回答

  • 2

    它创造了一个无限循环 .

    你正在监听 <Button1-Motion> ,当你得到它时,你会创建更多的 <Motion> while the Button is pressed (因为它会产生另一个 <Button1-Motion> 事件 . 所以再次调用该函数,依此类推 .

    <Motion>按住鼠标按钮移动鼠标 . 要指定鼠标左键,中键或右键,请分别使用<B1-Motion>,<B2-Motion>和<B3-Motion> . ...

    来自here .

相关问题