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('鼠标进

本文介绍了如何使用Python的Tkinter库绑定各种鼠标和键盘事件,包括鼠标左键按下、释放、移动,以及键盘的回车、Shift+F、Num_Lock等事件,同时展示了如何获取事件的相关属性。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



