Python tkinter之Bind(綁定事件)的使用示例
# -*- encoding=utf-8 -*-import tkinterfrom tkinter import *def left_mouse_down(event): print(’鼠標左鍵按下’) # 事件的屬性 widget = event.widget print(’觸發(fā)事件的組件:{}’.format(widget)) print(’組件顏色:{}’.format(widget.cget(’bg’))) widget_x = event.x # 相對于組件的橫坐標x print(’相對于組件的橫坐標:{}’.format(widget_x)) widget_y = event.y # 相對于組件的縱坐標y print(’相對于組件的縱坐標:{}’.format(widget_y)) x_root = event.x_root # 相對于屏幕的左上角的橫坐標 print(’相對于屏幕的左上角的橫坐標:{}’.format(x_root)) y_root = event.y_root # 相對于屏幕的左上角的縱坐標 print(’相對于屏幕的左上角的縱坐標:{}’.format(y_root))def left_mouse_up(event): print(’鼠標左鍵釋放’)def moving_mouse(event): print(’鼠標左鍵按下并移動’)def moving_into(event): print(’鼠標進入’)def moving_out(event): print(’鼠標移出’)def right_mouse_down(event): print(’鼠標右鍵按下’)def right_mouse_up(event): print(’鼠標右鍵釋放’)def pulley_up(event): print(’滑輪向上滾動’)def focus(event): print(’聚焦事件’)def unfocus(event): print(’失焦事件’)if __name__ == ’__main__’: win = tkinter.Tk() # 窗口 win.title(’南風丶輕語’) # 標題 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=’標簽’, relief=’g’, font=(’黑體’, 20)) label.pack(pady=10) label.bind(’<Button-1>’, left_mouse_down) # 鼠標左鍵按下 label.bind(’<ButtonRelease-1>’, left_mouse_up) # 鼠標左鍵釋放 label.bind(’<Button-3>’, right_mouse_down) # 鼠標右鍵按下 label.bind(’<ButtonRelease-3>’, right_mouse_up) # 鼠標右鍵釋放 label.bind(’<B1-Motion>’, moving_mouse) # 鼠標左鍵按下并移動 label.bind(’<Enter>’, moving_into) # 鼠標移入事件 label.bind(’<Leave>’, moving_out) # 鼠標移出事件 label.bind(’<FocusIn>’, focus) # 聚焦事件 label.bind(’<FocusOut>’, unfocus) # 失焦事件 label.focus_set() # 直接聚焦 Entry().pack() win.mainloop()
# -*- encoding=utf-8 -*-import tkinterfrom 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(’南風丶輕語’) # 標題 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=’標簽’, 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(綁定事件)的使用示例的詳細內(nèi)容,更多關于python tkinter Bind(綁定事件)的資料請關注好吧啦網(wǎng)其它相關文章!
相關文章:
1. idea設置提示不區(qū)分大小寫的方法2. docker容器調(diào)用yum報錯的解決辦法3. CentOS郵件服務器搭建系列—— POP / IMAP 服務器的構(gòu)建( Dovecot )4. HTTP協(xié)議常用的請求頭和響應頭響應詳解說明(學習)5. .NET SkiaSharp 生成二維碼驗證碼及指定區(qū)域截取方法實現(xiàn)6. Django使用HTTP協(xié)議向服務器傳參方式小結(jié)7. ASP.NET MVC通過勾選checkbox更改select的內(nèi)容8. css代碼優(yōu)化的12個技巧9. 原生JS實現(xiàn)記憶翻牌游戲10. django創(chuàng)建css文件夾的具體方法
