Python tkinter之Bind(綁定事件)的使用示例
1、綁定鼠標(biāo)事件并獲取事件屬性
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
def left_mouse_down(event):
print('鼠標(biāo)左鍵按下')
# 事件的屬性
widget = event.widget
print('觸發(fā)事件的組件:{}'.format(widget))
print('組件顏色:{}'.format(widget.cget('bg')))
widget_x = event.x # 相對(duì)于組件的橫坐標(biāo)x
print('相對(duì)于組件的橫坐標(biāo):{}'.format(widget_x))
widget_y = event.y # 相對(duì)于組件的縱坐標(biāo)y
print('相對(duì)于組件的縱坐標(biāo):{}'.format(widget_y))
x_root = event.x_root # 相對(duì)于屏幕的左上角的橫坐標(biāo)
print('相對(duì)于屏幕的左上角的橫坐標(biāo):{}'.format(x_root))
y_root = event.y_root # 相對(duì)于屏幕的左上角的縱坐標(biāo)
print('相對(duì)于屏幕的左上角的縱坐標(biāo):{}'.format(y_root))
def left_mouse_up(event):
print('鼠標(biāo)左鍵釋放')
def moving_mouse(event):
print('鼠標(biāo)左鍵按下并移動(dòng)')
def moving_into(event):
print('鼠標(biāo)進(jìn)入')
def moving_out(event):
print('鼠標(biāo)移出')
def right_mouse_down(event):
print('鼠標(biāo)右鍵按下')
def right_mouse_up(event):
print('鼠標(biāo)右鍵釋放')
def pulley_up(event):
print('滑輪向上滾動(dòng)')
def focus(event):
print('聚焦事件')
def unfocus(event):
print('失焦事件')
if __name__ == '__main__':
win = tkinter.Tk() # 窗口
win.title('南風(fēng)丶輕語') # 標(biāo)題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 500
height = 300
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
label = Label(text='標(biāo)簽', relief='g', font=('黑體', 20))
label.pack(pady=10)
label.bind('<Button-1>', left_mouse_down) # 鼠標(biāo)左鍵按下
label.bind('<ButtonRelease-1>', left_mouse_up) # 鼠標(biāo)左鍵釋放
label.bind('<Button-3>', right_mouse_down) # 鼠標(biāo)右鍵按下
label.bind('<ButtonRelease-3>', right_mouse_up) # 鼠標(biāo)右鍵釋放
label.bind('<B1-Motion>', moving_mouse) # 鼠標(biāo)左鍵按下并移動(dòng)
label.bind('<Enter>', moving_into) # 鼠標(biāo)移入事件
label.bind('<Leave>', moving_out) # 鼠標(biāo)移出事件
label.bind('<FocusIn>', focus) # 聚焦事件
label.bind('<FocusOut>', unfocus) # 失焦事件
label.focus_set() # 直接聚焦
Entry().pack()
win.mainloop()

2、綁定鍵盤事件并獲取事件屬性
# -*- encoding=utf-8 -*-
import tkinter
from tkinter import *
def keyboard_event(event):
char = event.char
print('回車 char:{}'.format(char))
key_code = event.keycode
print('回車 key code:{}'.format(key_code))
def entry_enter(event):
print('輸入的內(nèi)容為:' + entry.get())
def shift_f(event):
print('SHIFT + F')
print(event.char)
print(event.keycode)
def num_lock(event):
print('num_lock')
print(event.char)
print(event.keycode)
if __name__ == '__main__':
win = tkinter.Tk() # 窗口
win.title('南風(fēng)丶輕語') # 標(biāo)題
screenwidth = win.winfo_screenwidth() # 屏幕寬度
screenheight = win.winfo_screenheight() # 屏幕高度
width = 500
height = 300
x = int((screenwidth - width) / 2)
y = int((screenheight - height) / 2)
win.geometry('{}x{}+{}+{}'.format(width, height, x, y)) # 大小以及位置
label = Label(text='標(biāo)簽', relief='g', font=('黑體', 20))
label.pack(pady=10)
label.focus_set()
label.bind('<Return>', keyboard_event) # 按下回車
label.bind('<Shift F>', shift_f)
label.bind('<Num_Lock>', num_lock)
entry = Entry()
entry.pack()
entry.bind('<Return>', entry_enter) # 按下回車
win.mainloop()

以上就是Python tkinter之Bind(綁定事件)的使用示例的詳細(xì)內(nèi)容,更多關(guān)于python tkinter Bind(綁定事件)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
el-table 多表格彈窗嵌套數(shù)據(jù)顯示異常錯(cuò)亂問題解決方案
使用vue+element開發(fā)報(bào)表功能時(shí),需要列表上某列的超鏈接按鈕彈窗展示,在彈窗的el-table列表某列中再次使用超鏈接按鈕點(diǎn)開彈窗,以此類推多表格彈窗嵌套,本文以彈窗兩次為例,需要的朋友可以參考下2023-11-11
pytorch?K折交叉驗(yàn)證過程說明及實(shí)現(xiàn)方式
這篇文章主要介紹了pytorch?K折交叉驗(yàn)證過程說明及實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11
Python 3.6 -win64環(huán)境安裝PIL模塊的教程
PIL功能非常強(qiáng)大,但API卻非常簡單易用。這篇文章主要介紹了Python 3.6 -win64環(huán)境安裝PIL模塊的教程,需要的朋友可以參考下2019-06-06
使用Python快速實(shí)現(xiàn)文件共享并通過內(nèi)網(wǎng)穿透技術(shù)公網(wǎng)訪問
數(shù)據(jù)共享作為和連接作為互聯(lián)網(wǎng)的基礎(chǔ)應(yīng)用,不僅在商業(yè)和辦公場景有廣泛的應(yīng)用,對(duì)于個(gè)人用戶也有很強(qiáng)的實(shí)用意義,今天,筆者就為大家介紹,如何使用python這樣的簡單程序語言,在自己的電腦上搭建一個(gè)共享文件服務(wù)器,需要的朋友可以參考下2023-10-10
PyTorch中torch.save()的用法和應(yīng)用小結(jié)
本文主要介紹了PyTorch中torch.save()的用法和應(yīng)用小結(jié),torch.save()的主要作用就是將PyTorch對(duì)象保存到磁盤上,下面就來具體介紹一下,感興趣的可以了解一下2024-03-03
python中判斷數(shù)字是否為質(zhì)數(shù)的實(shí)例講解
在本篇文章里小編給大家分享了關(guān)于python中判斷數(shù)字是否為質(zhì)數(shù)的實(shí)例講解內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2020-12-12
基于DataFrame篩選數(shù)據(jù)與loc的用法詳解
今天小編就為大家分享一篇基于DataFrame篩選數(shù)據(jù)與loc的用法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-05-05

