Python tkinter之Bind(綁定事件)的使用示例
更新時(shí)間:2021年02月05日 11:49:32 作者:南風(fēng)丶輕語(yǔ)
這篇文章主要介紹了Python tkinter之Bind(綁定事件)的使用詳解,幫助大家更好的理解和學(xué)習(xí)python的gui開(kāi)發(fā),感興趣的朋友可以了解下
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)丶輕語(yǔ)') # 標(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、綁定鍵盤(pán)事件并獲取事件屬性
# -*- 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)丶輕語(yǔ)') # 標(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)文章!
您可能感興趣的文章:
- python使用tkinter實(shí)現(xiàn)屏幕中間倒計(jì)時(shí)
- Python使用tkinter實(shí)現(xiàn)小時(shí)鐘效果
- Python tkinter實(shí)現(xiàn)日期選擇器
- Python使用tkinter制作在線翻譯軟件
- 用python制作個(gè)音樂(lè)下載器
- 基于python實(shí)現(xiàn)的百度音樂(lè)下載器python pyqt改進(jìn)版(附代碼)
- python實(shí)現(xiàn)音樂(lè)下載的統(tǒng)計(jì)
- python實(shí)現(xiàn)音樂(lè)下載器
- python基于tkinter制作無(wú)損音樂(lè)下載工具(附源碼)
相關(guān)文章
Python灰度變換中灰度切割分析實(shí)現(xiàn)
灰度變換是指根據(jù)某種目標(biāo)條件按一定變換關(guān)系逐點(diǎn)改變?cè)磮D像中每個(gè)像素灰度值的方法。目的是改善畫(huà)質(zhì),使圖像顯示效果更加清晰。圖像的灰度變換處理是圖像增強(qiáng)處理技術(shù)中的一種非?;A(chǔ)、直接的空間域圖像處理方法,也是圖像數(shù)字化軟件和圖像顯示軟件的一個(gè)重要組成部分2022-10-10Python實(shí)現(xiàn)生活常識(shí)解答機(jī)器人
今天教大家如何用Python爬蟲(chóng)去搭建一個(gè)「生活常識(shí)解答」機(jī)器人.思路:這個(gè)機(jī)器人主要是依托于“阿里達(dá)摩院發(fā)布的語(yǔ)言模型PLUG”,通過(guò)爬蟲(chóng)的方式,發(fā)送post請(qǐng)求(提問(wèn)),然后返回json數(shù)據(jù)(回答),需要的朋友可以參考下2021-06-06python?pip安裝庫(kù)下載源更換(清華源、阿里源、中科大源、豆瓣源)
為了提高Python包的下載速度和穩(wěn)定性,可以配置國(guó)內(nèi)的鏡像源,如清華源、阿里源、中科大源和豆瓣源,設(shè)置方法簡(jiǎn)單,只需更改pip的配置文件或使用命令行即可,需要的朋友可以參考下2024-10-10基于Python實(shí)現(xiàn)的掃雷游戲?qū)嵗a
這篇文章主要介紹了基于Python實(shí)現(xiàn)的掃雷游戲?qū)嵗a,對(duì)于Python的學(xué)習(xí)以及Python游戲開(kāi)發(fā)都有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08Python如何爬取b站熱門(mén)視頻并導(dǎo)入Excel
這篇文章主要介紹了Python如何爬取b站熱門(mén)視頻并導(dǎo)入Excel,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08keras 模型參數(shù),模型保存,中間結(jié)果輸出操作
這篇文章主要介紹了keras 模型參數(shù),模型保存,中間結(jié)果輸出操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-07-07