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

 更新时间:2021年02月05日 11:49:32   作者:南风丶轻语  
这篇文章主要介绍了Python tkinter之Bind(绑定事件)的使用详解,帮助大家更好的理解和学习python的gui开发,感兴趣的朋友可以了解下

1、绑定鼠标事件并获取事件属性

# -*- encoding=utf-8 -*-

import tkinter
from tkinter import *


def left_mouse_down(event):
  print('鼠标左键按下')

  # 事件的属性
  widget = event.widget
  print('触发事件的组件:{}'.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()

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('输入的内容为:' + 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(绑定事件)的使用示例的详细内容,更多关于python tkinter Bind(绑定事件)的资料请关注脚本之家其它相关文章!

相关文章

  • el-table 多表格弹窗嵌套数据显示异常错乱问题解决方案

    el-table 多表格弹窗嵌套数据显示异常错乱问题解决方案

    使用vue+element开发报表功能时,需要列表上某列的超链接按钮弹窗展示,在弹窗的el-table列表某列中再次使用超链接按钮点开弹窗,以此类推多表格弹窗嵌套,本文以弹窗两次为例,需要的朋友可以参考下
    2023-11-11
  • pytorch K折交叉验证过程说明及实现方式

    pytorch K折交叉验证过程说明及实现方式

    这篇文章主要介绍了pytorch K折交叉验证过程说明及实现方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11
  • Python 3.6 -win64环境安装PIL模块的教程

    Python 3.6 -win64环境安装PIL模块的教程

    PIL功能非常强大,但API却非常简单易用。这篇文章主要介绍了Python 3.6 -win64环境安装PIL模块的教程,需要的朋友可以参考下
    2019-06-06
  • 使用Python快速实现文件共享并通过内网穿透技术公网访问

    使用Python快速实现文件共享并通过内网穿透技术公网访问

    数据共享作为和连接作为互联网的基础应用,不仅在商业和办公场景有广泛的应用,对于个人用户也有很强的实用意义,今天,笔者就为大家介绍,如何使用python这样的简单程序语言,在自己的电脑上搭建一个共享文件服务器,需要的朋友可以参考下
    2023-10-10
  • PyTorch中torch.save()的用法和应用小结

    PyTorch中torch.save()的用法和应用小结

    本文主要介绍了PyTorch中torch.save()的用法和应用小结,torch.save()的主要作用就是将PyTorch对象保存到磁盘上,下面就来具体介绍一下,感兴趣的可以了解一下
    2024-03-03
  • 如何利用python倒置所输字符串的顺序

    如何利用python倒置所输字符串的顺序

    这篇文章主要介绍了如何利用python倒置所输字符串的顺序问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2024-03-03
  • 一文解决Python切换版本问题

    一文解决Python切换版本问题

    由于mac默认都会安装python2.x,这给我们python开发造成不便,我们经常要用到python3.x的版本来进行测试、开发,所以本文主要介绍了Python切换版本问题,感兴趣的可以了解一下
    2021-07-07
  • python中判断数字是否为质数的实例讲解

    python中判断数字是否为质数的实例讲解

    在本篇文章里小编给大家分享了关于python中判断数字是否为质数的实例讲解内容,有兴趣的朋友们可以学习下。
    2020-12-12
  • 基于DataFrame筛选数据与loc的用法详解

    基于DataFrame筛选数据与loc的用法详解

    今天小编就为大家分享一篇基于DataFrame筛选数据与loc的用法详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-05-05
  • python基础之多态

    python基础之多态

    这篇文章主要介绍了python多态,实例分析了Python中返回一个返回值与多个返回值的方法,需要的朋友可以参考下
    2021-10-10

最新评论