2016-06-12 110 views
1

我試圖用一些方法綁定鼠標運動(按下/未按下)。 我試圖處理鼠標的動作,而鼠標按鈕用''鍵和另一隻用''鍵。 我發現,當我剛剛有..bind('',somemethod1),somemethod1被稱爲不管鼠標按鈕按下,但是當我也有..bind('',somemethod2),somemethod1不被稱爲鼠標按鈕時按下。 添加'add ='+''似乎不起作用。tkinter最大遞歸深度超過生成事件

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) 遞歸錯誤:超過最大遞歸深度

任何人都可以向我解釋爲什麼會發生這種情況嗎? 我知道我可以通過調用on_button1_motion1方法內的on_button1_motion1方法而不是生成事件來解決此問題,但我想知道爲什麼其他方式不起作用。謝謝

回答

2

它創建一個無限循環。

您現在收聽的<Button1-Motion>,當你得到它,你創造更多的<Motion>當按下按鈕(因爲它時,抓住了按鈕1事件僅生成)。所以你正在生成另一個<Button1-Motion>事件。所以函數再次被調用,等等。

<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

+0

嗯,你的意思是當我在聽''Button1-Motion>'事件時創建''事件,on_button1_motion1因爲「偵聽寄存器」而被調用?有趣。我從來不知道這一點。 –

+0

當偵聽該事件的命令(方法)執行時(返回之前)是否「偵聽」? –

+0

對不起,我犯了一個錯誤:''並不總是觸發''的回調。我重新創建了你的程序並發現了相同的行爲。問題在於當按鈕已經被按下時產生''事件**產生另一個''事件(因爲按鈕被按下並且你正在產生Motion!)。回答最後一個問題:是的,在執行回調函數'on_button1_motion1'時,小部件仍在偵聽更多事件,並且在按下按鈕時又創建了另一個''事件,因此它是'< Button1的迅>」。 – krork