from Tkinter import *
root = Tk()
def eventHandler(event):
print((event.x, event.y))
canvas=Canvas(root, width=100, height=100)
root.bind("", eventHandler)
root.pack()
root.mainloop()
一、事件(event)
(1)事件格式:
在Tkinter中,事件的描述格式为:<[modifier-]-type[-detail]>,其中:
☆☆☆注意大小写!!!
事件类型 | 事件格式 | 事件解释 |
鼠标事件 | 鼠标点击(1-左键,2-中键,3-右键) | |
鼠标双击(1-左键,2-中键,3-右键) | ||
鼠标拖动(1-左键,2-中键,3-右键) | ||
鼠标按下之后释放(1-左键,2-中键,3-右键) | ||
鼠标进入控件范围(widget),不是键盘按键 | ||
鼠标离开控件范围(widget) | ||
键盘事件 | 任意键盘按键(键值会以char的格式放入event对象) | |
|
对应键盘按键 | |
组件事件 | 如果widget的大小发生改变,新的大小(width和height)会打包到event发往handler。 | |
当组件从不可用变为可用 | ||
当组件从可用变为不可用 | ||
当组件被销毁时 | ||
当组件从被遮挡状态变为暴露状态 | ||
当组件由隐藏状态变为显示状态 | ||
当组件由显示状态变为隐藏状态 | ||
当组件获得焦点时 | ||
当组件失去焦点时 |
||
当组件属性发生改变时 | ||
当组件变为可视状态时 |
(2)事件对象:
一个具体事件如
属性 | 属性说明 | 适用事件类型 |
.char | 如果按键事件产生通用ASCII字符,这个字符将赋值给event.char。(特殊ASCII字符,如delete等不属于该属性) |
|
.keysym | 如果按键事件产生特殊ASCII字符,这个字符将赋值给event.keysym。 | |
.x | 鼠标当前位置横坐标,相对于组件左上角 | |
.y | 鼠标当前位置纵坐标,相对于组件左上角 | |
.x_root | 鼠标当前位置横坐标,相对于屏幕左上角 | |
.y_root | 鼠标当前位置纵坐标,相对于屏幕左上角 | |
.width | 组件大小发生改变后的宽度 | |
.height | 组件大小发生改变后的高度 | |
.type | 事件类型 | ALL |
二、基于事件的动画(animation)
(1)模型-视图-控制器(MVC,Model、View and Controller)
MVC是事件循环运行动画的三个核心内容:
(2)MVC必须遵循如下因果关系:(C→M→V)
(3)一个例子:
# Basic Animation Framework
from tkinter import *
####################################
# customize these functions
####################################
# Set up the model data with init
# init is called once, at the beginning of the program
# data is a Struct, which can be given new data values using data.name = value
# data will be shared across all animation functions- it's aliased!
def init(data):
# data comes preset with width and height, from the run function
data.circleSize = min(data.width, data.height) / 10
data.circleX = data.width/2
data.circleY = data.height/2
data.charText = ""
data.keysymText = ""
# Track and respond to mouse clicks
# The event variable holds all of the data captured by the event loop
# For mousePressed, this is event.x and event.y, the position where
# the mouse was clicked
def mousePressed(event, data):
data.circleX = event.x
data.circleY = event.y
# Track and respond to key presses
# The event variable holds all of the data captured by the event loop
# For keyPressed, this is event.char and event.keysym
# event.char holds the direct key that was pressed, "a", "3", "@", etc.
# event.keysym holds special names for certain keys non-alphanumeric keys
# for example, "space", "BackSpace", "parenleft", "exclam"
def keyPressed(event, data):
data.charText = event.char
data.keysymText = event.keysym
# Draw graphics normally with redrawAll
# Main difference: the data struct contains helpful information to assist drawing
# Also, the canvas will get cleared and this will be called again
# constantly by the event loop.
def redrawAll(canvas, data):
canvas.create_oval(data.circleX - data.circleSize,
data.circleY - data.circleSize,
data.circleX + data.circleSize,
data.circleY + data.circleSize)
if data.charText != "":
canvas.create_text(data.width/10, data.height/3,
text="char: " + data.charText)
if data.keysymText != "":
canvas.create_text(data.width/10, data.height*2/3,
text="keysym: " + data.keysymText)
####################################
# use the run function as-is
####################################
def run(width=300, height=300):
def redrawAllWrapper(canvas, data):
canvas.delete(ALL)
canvas.create_rectangle(0, 0, data.width, data.height,
fill='white', width=0)
redrawAll(canvas, data)
canvas.update()
def mousePressedWrapper(event, canvas, data):
mousePressed(event, data)
redrawAllWrapper(canvas, data)
def keyPressedWrapper(event, canvas, data):
keyPressed(event, data)
redrawAllWrapper(canvas, data)
# Set up data and call init
class Struct(object): pass
data = Struct()
data.width = width
data.height = height
root = Tk()
init(data)
# create the root and the canvas
canvas = Canvas(root, width=data.width, height=data.height)
canvas.pack()
# set up events
root.bind("", lambda event:
mousePressedWrapper(event, canvas, data))
root.bind("", lambda event:
keyPressedWrapper(event, canvas, data))
redrawAll(canvas, data)
# and launch the app
root.mainloop() # blocks until window is closed
print("bye!")
run(400, 200)