生成偶数时超出了tkinter最大递归深度

2024-04-25 04:01:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试用一些方法绑定鼠标运动(按下/未按下)。 我试着处理鼠标的动作,而鼠标按键是按“”的,另一个只按''。 我发现当我有..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\u motion1方法,如下所示:

^{2}$

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

回溯(最近一次呼叫): 文件“D:/save/WORKSHOP/py/tkinter/Blueprints/Pycrosoft Paintk/视图.py“,第107行,英寸 根.mainloop() 文件“C:\Users\smj\AppData\Local\Programs\Python\Python35\lib\tkinter_yuinit_u.py”,第1131行,在主循环中 self.tk.mainloop(n) 递归错误:超过最大递归深度

有人能解释一下为什么会这样吗? 我知道我可以解决这个问题,只要调用“on”button1“上的”mouse“unpressed”的“motion1”方法,而不是生成一个事件,但我想知道为什么另一种方法不起作用。谢谢


Tags: 方法pyselfeventbindondef鼠标
1条回答
网友
1楼 · 发布于 2024-04-25 04:01:26

它创造了一个无限循环。在

您正在监听<Button1-Motion>,当您获得它时,您将在按下按钮时创建更多的<Motion>(因为它只在捕捉到Button-1事件时生成)。所以您正在生成另一个事件。所以函数被再次调用,以此类推。在

<Motion>

The mouse is moved with a mouse button being held down. To specify the left, middle or right mouse button use <B1-Motion>, <B2-Motion> and <B3-Motion> respectively.

...

来自here。在

相关问题 更多 >