網站首頁 編程語言 正文
1.事件綁定函數
事件綁定函數有三個如下:? ? ? ?
組件.bind('事件類型',事件函數)
為一個組件綁定一個操作
組件.bind_class('組件類型','事件類型',事件函數)
為一個類組件綁定一個操作
組件.bind_all('事件類型',事件函數)
為所有組件綁定一個操作(所有操作都會當作對主界面的操作)
2.事件類型
3.事件對象
事件綁定的案例1:
鼠標進入組件變紅,離開組件變白:
# 單行文本輸入框 entry = tkinter.Entry(root) entry.pack() # 事件函數 def changered(eventobj): ? ? # 通過事件對象獲取得到組件 ? ? eventobj.widget['bg'] = 'red'#鼠標進入組件變紅 def wdc(eventobj): ? ? eventobj.widget['bg'] = 'white'#鼠標離開組件變白 # 事件綁定 entry.bind('',changered) entry.bind(' ',wdc)
注意:通過綁定函數操作,對應的事件函數,必須有形參接受事件對象。
事件函數必須在綁定之前定義
事件綁定的案例2:
鍵盤快捷鍵觸發事件:按快捷鍵改變窗口背景色
import tkinter # 創建主窗口 root = tkinter.Tk() # 設置窗口大小 root.minsize(300,200) # 變紅色 def changer(eventobj): ? ? eventobj.widget['bg'] = 'red' # 按Ctrl + r窗口變紅色:兩條命令 R和r都行 root.bind('',changer) root.bind(' ',changer) # 加入消息循環 root.mainloop()
事件綁定的案例3:(為一類組件綁定事件)
import tkinter # 創建主窗口 root = tkinter.Tk() # 設置窗口大小 root.minsize(300,200) # 按鈕1 btn1 = tkinter.Button(root,text = '1') btn1.place(x = 20,y = 20,width = 40,height = 40) # 按鈕2 btn2 = tkinter.Button(root,text = '2') btn2.place(x = 80,y = 20,width = 40,height = 40) # 按鈕3 btn3 = tkinter.Button(root,text = '3') btn3.place(x = 140,y = 20,width = 40,height = 40) # 按鈕4 btn4 = tkinter.Button(root,text = '4') btn4.place(x = 20,y = 80,width = 40,height = 40) # 按鈕5 btn5 = tkinter.Button(root,text = '5') btn5.place(x = 80,y = 80,width = 40,height = 40) # 按鈕6 btn6 = tkinter.Button(root,text = '6') btn6.place(x = 140,y = 80,width = 40,height = 40) def changebg(wdc): ? ? # 鼠標放到按鈕上按鈕變紅 ? ? wdc.widget['bg'] = 'red' def changebg1(wdc): ? ? # 鼠標離開按鈕上按鈕變白 ? ? wdc.widget['bg'] = 'white' # 綁定按鈕鼠標進入事件 btn1.bind_class('Button','',changebg) btn1.bind_class('Button',' ',changebg1) # 加入消息循環 root.mainloop()
事件綁定的案例4:(為所有組件綁定事件)
鼠標點擊所有組件,Entry組件背景顏色都會變紅:
import tkinter # 創建主窗口 root = tkinter.Tk() # 設置窗口大小 root.minsize(300,200) # 按鈕 btn1 = tkinter.Button(root,text = '###############') btn1.pack() # 輸入框 entry = tkinter.Entry(root) entry.pack() # 多行輸入框 text = tkinter.Text(root,width = 20,height = 5) text.pack() # 函數 def changeentry(e): ? ? # 鼠標點擊任意組件,Entry組件都會變紅 ? ? entry['bg'] = 'red' # 事件綁定 btn1.bind_all('',changeentry) # 加入消息循環 root.mainloop()
原文鏈接:https://www.cnblogs.com/wangdianchao/p/11555130.html
相關推薦
- 2022-09-15 c++中的字節序與符號位的問題_C 語言
- 2023-02-01 Python?asyncore?socket客戶端開發基本使用教程_python
- 2022-03-27 pygame實現貪吃蛇游戲_python
- 2022-12-29 python中的多進程的創建與啟動方式_python
- 2022-03-22 C++類的定義與實現_C 語言
- 2023-05-12 Python?pandas?的索引方式?data.loc[],data[][]示例詳解_python
- 2022-12-05 Android自定義view實現側滑欄詳解_Android
- 2022-12-07 Android?Flutter實現興趣標簽選擇功能_Android
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支