婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > Python tkinter之Bind(綁定事件)的使用示例

Python tkinter之Bind(綁定事件)的使用示例

熱門標(biāo)簽:天津塘沽區(qū)地圖標(biāo)注 如何申請400電話代理 地圖標(biāo)注可以遠(yuǎn)程操作嗎 江門智能電話機器人 甘肅高頻外呼系統(tǒng) 杭州房產(chǎn)地圖標(biāo)注 400電話在線如何申請 滴滴地圖標(biāo)注公司 智能電話機器人調(diào)研

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)進入')
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)文章!

您可能感興趣的文章:
  • python使用tkinter實現(xiàn)屏幕中間倒計時
  • Python使用tkinter實現(xiàn)小時鐘效果
  • Python tkinter實現(xiàn)日期選擇器
  • Python使用tkinter制作在線翻譯軟件
  • 用python制作個音樂下載器
  • 基于python實現(xiàn)的百度音樂下載器python pyqt改進版(附代碼)
  • python實現(xiàn)音樂下載的統(tǒng)計
  • python實現(xiàn)音樂下載器
  • python基于tkinter制作無損音樂下載工具(附源碼)

標(biāo)簽:重慶 臨汾 長春 河池 東莞 漢中 廊坊 德宏

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Python tkinter之Bind(綁定事件)的使用示例》,本文關(guān)鍵詞  Python,tkinter,之,Bind,綁定,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Python tkinter之Bind(綁定事件)的使用示例》相關(guān)的同類信息!
  • 本頁收集關(guān)于Python tkinter之Bind(綁定事件)的使用示例的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    主站蜘蛛池模板: 北流市| 乌鲁木齐县| 北宁市| 贵南县| 岑溪市| 呼图壁县| 垫江县| 庆阳市| 赤壁市| 武穴市| 秦安县| 南和县| 大宁县| 通海县| 本溪| 遂平县| 个旧市| 内江市| 凤山县| 涪陵区| 许昌市| 桃园县| 通州市| 察隅县| 易门县| 宁津县| 大方县| 精河县| 祁东县| 南安市| 呼图壁县| 德安县| 闽清县| 平武县| 兴宁市| 肃宁县| 阿荣旗| 乐业县| 西畴县| 绵竹市| 商丘市|