Python tkinter之Bind(綁定事件)的使用示例
更新時(shí)間:2021年02月05日 11:49:32 作者:南風(fēng)丶輕語
這篇文章主要介紹了Python tkinter之Bind(綁定事件)的使用詳解,幫助大家更好的理解和學(xué)習(xí)python的gui開發(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 # 相對于組件的橫坐標(biāo)x print('相對于組件的橫坐標(biāo):{}'.format(widget_x)) widget_y = event.y # 相對于組件的縱坐標(biāo)y print('相對于組件的縱坐標(biāo):{}'.format(widget_y)) x_root = event.x_root # 相對于屏幕的左上角的橫坐標(biāo) print('相對于屏幕的左上角的橫坐標(biāo):{}'.format(x_root)) y_root = event.y_root # 相對于屏幕的左上角的縱坐標(biāo) print('相對于屏幕的左上角的縱坐標(biāo):{}'.format(y_root)) def left_mouse_up(event): print('鼠標(biāo)左鍵釋放') def moving_mouse(event): print('鼠標(biāo)左鍵按下并移動') 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('滑輪向上滾動') 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)左鍵按下并移動 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(綁定事件)的資料請關(guān)注腳本之家其它相關(guān)文章!
您可能感興趣的文章:
相關(guān)文章
Python灰度變換中灰度切割分析實(shí)現(xiàn)
灰度變換是指根據(jù)某種目標(biāo)條件按一定變換關(guān)系逐點(diǎn)改變源圖像中每個(gè)像素灰度值的方法。目的是改善畫質(zhì),使圖像顯示效果更加清晰。圖像的灰度變換處理是圖像增強(qiáng)處理技術(shù)中的一種非?;A(chǔ)、直接的空間域圖像處理方法,也是圖像數(shù)字化軟件和圖像顯示軟件的一個(gè)重要組成部分2022-10-10Python實(shí)現(xiàn)生活常識解答機(jī)器人
今天教大家如何用Python爬蟲去搭建一個(gè)「生活常識解答」機(jī)器人.思路:這個(gè)機(jī)器人主要是依托于“阿里達(dá)摩院發(fā)布的語言模型PLUG”,通過爬蟲的方式,發(fā)送post請求(提問),然后返回json數(shù)據(jù)(回答),需要的朋友可以參考下2021-06-06python?pip安裝庫下載源更換(清華源、阿里源、中科大源、豆瓣源)
為了提高Python包的下載速度和穩(wěn)定性,可以配置國內(nèi)的鏡像源,如清華源、阿里源、中科大源和豆瓣源,設(shè)置方法簡單,只需更改pip的配置文件或使用命令行即可,需要的朋友可以參考下2024-10-10基于Python實(shí)現(xiàn)的掃雷游戲?qū)嵗a
這篇文章主要介紹了基于Python實(shí)現(xiàn)的掃雷游戲?qū)嵗a,對于Python的學(xué)習(xí)以及Python游戲開發(fā)都有一定的借鑒價(jià)值,需要的朋友可以參考下2014-08-08keras 模型參數(shù),模型保存,中間結(jié)果輸出操作
這篇文章主要介紹了keras 模型參數(shù),模型保存,中間結(jié)果輸出操作,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07