python gui之tkinter事件處理


事件一覽表
事件 代碼 備注
鼠標左鍵單擊按下 1/Button-1/ButtonPress-1  
鼠標左鍵單擊松開 ButtonRelease-1  
鼠標右鍵單擊 3  
鼠標左鍵雙擊 Double-1/Double-Button-1  
鼠標右鍵雙擊 Double-3  
鼠標滾輪單擊 2  
鼠標滾輪雙擊 Double-2  
鼠標移動 B1-Motion  
鼠標移動到區域 Enter  
鼠標離開區域 Leave  
獲得鍵盤焦點 FocusIn  
失去鍵盤焦點 FocusOut  
鍵盤事件 Key  
回車鍵 Return  
控件尺寸變 Configure  

 

響應時間

提前響應

ttk treeview的TreeviewSelect事件是提前的,即你選中了某行,通過treeview.selection()[0]得到的就是這一樣。

 

延后相應

比如ttk的treeview是的單擊的情況,單擊的行被選中了,但是通過 treeview.selection()[0]得到的卻不是選中的行!而是之前選中的行。可以參考下這里

響應函數

event_handler(event,*args)

event參數

event 參數有以下屬性:

['__doc__', '__module__', 'char', 'delta', 'height', 'keycode', 'keysym', 'keysym_num', 'num', 'send_event', 'serial', 'state', 'time', 'type', 'widget', 'width', 'x', 'x_root', 'y', 'y_root']

Event Attributes

widget

The widget which generated this event. This is a valid Tkinter widget instance, not a name. This attribute is set for all events.

x, y

鼠標當前的相對位置,以像素為單位。

比如,ttk treeview 有個通過y坐標定位行的方法:identify_row(self, y)

 

x_root, y_root

鼠標當前的絕對位置(相對於設備的左上角)。以像素為單位。

char

字符(鍵盤事件中才有), 類型是字符串。

keysym

The key symbol (keyboard events only).

鍵符(鍵盤事件中才有)

keycode

鍵碼 (鍵盤事件中才有).

num

按鈕號碼(鼠標事件中才有)1-左鍵/2-中/3-右

width, height

widget的新尺寸,以像素為單位(Configure events only).

type
        事件類型
        1---
        2---
        3---
        4---鼠標
更多資料參考 這里

綁定事件

控件.bind('<事件代碼>',event_handler)

適用於大多數控件。此外還有bind_all方法。

控件.protocal('事件代碼', event_handler)

這種情況的控件,必需是頂層窗口或者root容器。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM