給控件綁定事件洒疚,當(dāng)發(fā)生該事件時(shí)羡蛾,調(diào)用函數(shù)
widget.bind('<event>', function)
Event事件
語(yǔ)法格式
<modifier-modifier-type-detail>
modifier修飾符
修飾符 |
說(shuō)明 |
Control |
按下ctrl鍵 |
Alt |
按下Alt鍵 |
Shift |
按下shift鍵 |
Lock |
按下CapsLock鍵 |
Button1, B1 |
鼠標(biāo)左鍵按下 |
Button2, B2 |
鼠標(biāo)滾輪按下 |
Button3, B3 |
鼠標(biāo)右鍵按下 |
Double |
連續(xù)發(fā)生兩次 |
Triple |
連續(xù)發(fā)生三次 |
Quadruple |
連續(xù)發(fā)生四次 |
type事件類(lèi)型
Type |
Descirption |
Activate |
當(dāng)組件激活時(shí)觸發(fā) |
Deactivate |
當(dāng)組件停用時(shí)觸發(fā) |
Destroy |
當(dāng)組件摧毀時(shí)觸發(fā) |
Map |
當(dāng)組件由隱藏狀態(tài)變?yōu)轱@示狀態(tài)時(shí)觸發(fā) |
Unmap |
當(dāng)組件由顯示狀態(tài)變?yōu)殡[藏狀態(tài)時(shí)觸發(fā) |
ButtonPress, Button |
當(dāng)組件按下時(shí)觸發(fā) |
ButtonRelease |
當(dāng)組件彈起時(shí)觸發(fā) |
Enter |
表示鼠標(biāo)光標(biāo)進(jìn)入某個(gè)組件時(shí)觸發(fā) |
Leave |
表示鼠標(biāo)光標(biāo)離開(kāi)某個(gè)組件時(shí)觸發(fā) |
Expose |
當(dāng)組件從被遮擋狀態(tài)中暴露出來(lái)時(shí)觸發(fā) |
Motion |
只要指針移動(dòng)卧晓,就會(huì)生成運(yùn)動(dòng)事件 |
FocusIn |
當(dāng)組件獲得焦點(diǎn)時(shí)觸發(fā) |
FocusOut |
當(dāng)組件失去焦點(diǎn)時(shí)觸發(fā) |
MouseWheel |
表示鼠標(biāo)滑輪滾動(dòng)操作 |
Property |
當(dāng)窗體的屬性被修改或者刪除時(shí)觸發(fā) |
Colormap |
每當(dāng)與窗口關(guān)聯(lián)的顏色圖被更改顷窒、安裝或卸載時(shí)觸發(fā) |
Configure |
當(dāng)組件大小改變時(shí)觸發(fā) |
KeyPress, Key |
當(dāng)同時(shí)按下姊途,如:<Alt-KeyPress-A>仍侥,表示當(dāng)同時(shí)按下Alt 和 A鍵時(shí)觸發(fā) |
KeyRelease |
同時(shí)放開(kāi)鍵 |
Visibility |
當(dāng)組件變?yōu)榭梢暊顟B(tài)時(shí)觸發(fā) |
實(shí)例
import tkinter as tk
import tkinter.messagebox
class App(tk.Tk):
def __init__(self):
super().__init__()
self.geometry('600x400')
self.set_widget()
self.bind_event()
self.mainloop()
def set_widget(self):
self.frm = tk.Frame(self)
self.frm.pack()
tk.Label(self.frm,text='賬號(hào)').grid(row=0,column=0)
tk.Label(self.frm,text='密碼').grid(row=1,column=0,pady=10)
self.tip = tk.StringVar()
self.tip.set('請(qǐng)輸入郵箱或者手機(jī)號(hào)')
self.ety_name = tk.Entry(self.frm,textvariable=self.tip,fg='gray')
self.ety_name.grid(row=0,column=1,columnspan=3)
self.ety_pwd = tk.Entry(self.frm)
self.ety_pwd.grid(row=1,column=1,columnspan=3)
self.btn_yes = tk.Button(self.frm,text='確定(y)',width=8,command=self.show,underline=3)
self.btn_yes.grid(row=2,column=3)
self.btn_cancel = tk.Button(self.frm,text='取消(d)',width=8,underline=3,command=self.show2)
self.btn_cancel.grid(row=2,column=1)
def bind_event(self):
self.ety_name.bind('<FocusIn>',self.delete_tip)
self.ety_name.bind('<FocusOut>',self.add_tip)
self.ety_pwd.bind('<Return>',self.__show) # 當(dāng)在密碼輸入框輸入完,按回車(chē)鍵就可以捉偏,觸發(fā)函數(shù)倒得。點(diǎn)擊了確定按鈕
# 設(shè)置快捷鍵
self.bind('<Control-y>',self.__show)
self.bind('<Control-d>',self.__show2)
def add_tip(self,evnet):
ety_name = self.ety_name.get()
if ety_name == '':
self.tip.set('請(qǐng)輸入郵箱或者手機(jī)號(hào)')
self.ety_name.config(fg='gray')
tkinter.messagebox.showerror(message='賬號(hào)不能為空')
def delete_tip(self,event):
ety_name = self.ety_name.get()
if ety_name == '請(qǐng)輸入郵箱或者手機(jī)號(hào)':
self.ety_name.delete(0,'end')
self.ety_name.config(fg='black')
def show(self):
name = self.ety_name.get()
tkinter.messagebox.showinfo(message=f'welecom {name}')
def __show(self,event):
self.show()
def show2(self):
tkinter.messagebox.showinfo(message='you press cancel')
def __show2(self,event):
self.show2()
App()
Event事件對(duì)象
當(dāng)事件觸發(fā)后,Tkinter 會(huì)自動(dòng)將事件對(duì)象交給回調(diào)函數(shù)進(jìn)行下步的處理夭禽,Event 對(duì)象包含了以下常用屬性:
屬性 |
說(shuō)明 |
widget |
發(fā)生事件的是哪一個(gè)控件 |
x,y |
相對(duì)于窗口的左上角而言霞掺,當(dāng)前鼠標(biāo)的坐標(biāo)位置 |
x_root,y_root |
相對(duì)于屏幕的左上角而言,當(dāng)前鼠標(biāo)的坐標(biāo)位置 |
char |
用來(lái)顯示所按鍵相對(duì)應(yīng)的字符 |
keysym |
按鍵名讹躯,比如 Control_L 表示左邊的 Ctrl 按鍵 |
keycode |
按鍵碼菩彬,一個(gè)按鍵的數(shù)字編號(hào),比如 Delete 按鍵碼是107 |
num |
1/2/3中的一個(gè)潮梯,表示點(diǎn)擊了鼠標(biāo)的哪個(gè)按鍵骗灶,按鍵分為左、中秉馏、右 |
width,height |
控件的修改后的尺寸耙旦,對(duì)應(yīng)著 <Configure>事件 |
type |
事件類(lèi)型 |
type對(duì)應(yīng)的數(shù)字
KeyPress = '2'
Key = KeyPress,
KeyRelease = '3'
ButtonPress = '4'
Button = ButtonPress,
ButtonRelease = '5'
Motion = '6'
Enter = '7'
Leave = '8'
FocusIn = '9'
FocusOut = '10'
Keymap = '11' # undocumented
Expose = '12'
GraphicsExpose = '13' # undocumented
NoExpose = '14' # undocumented
Visibility = '15'
Create = '16'
Destroy = '17'
Unmap = '18'
Map = '19'
MapRequest = '20'
Reparent = '21'
Configure = '22'
ConfigureRequest = '23'
Gravity = '24'
ResizeRequest = '25'
Circulate = '26'
CirculateRequest = '27'
Property = '28'
SelectionClear = '29' # undocumented
SelectionRequest = '30' # undocumented
Selection = '31' # undocumented
Colormap = '32'
ClientMessage = '33' # undocumented
Mapping = '34' # undocumented
VirtualEvent = '35', # undocumented
Activate = '36',
Deactivate = '37',
MouseWheel = '38',
這樣使用上面的type對(duì)應(yīng)數(shù)字,就不用寫(xiě)多個(gè)函數(shù)來(lái)處理事件萝究。