'''Tkinter教程之Event篇(1)'''
# 事件的使用方法
'''1.測試滑鼠點擊(Click)事件'''
# -*- coding: cp936 -*-
# <Button-1>:滑鼠左擊事件
# <Button-2>:滑鼠中擊事件
# <Button-3>:滑鼠右擊事件
# <Double-Button-1>:雙擊事件
# <Triple-Button-1>:三擊事件
from Tkinter import *
root = Tk()
def printCoords(event):
print event.x,event.y
# 建立第一個Button,並將它與左鍵事件綁定
bt1 = Button(root,text = 'leftmost button')
bt1.bind('<Button-1>',printCoords)
# 建立二個Button,並將它與中鍵事件綁定
bt2 = Button(root,text = 'middle button')
bt2.bind('<Button-2>',printCoords)
# 建立第三個Button,並將它與右擊事件綁定
bt3 = Button(root,text = 'rightmost button')
bt3.bind('<Button-3>',printCoords)
# 建立第四個Button,並將它與雙擊事件綁定
bt4 = Button(root,text = 'double click')
bt4.bind('<Double-Button-1>',printCoords)
# 建立第五個Button,並將它與三擊事件綁定
bt5 = Button(root, text = 'triple click')
bt5.bind('<Triple-Button-1>',printCoords)
bt1.grid()
bt2.grid()
bt3.grid()
bt4.grid()
bt5.grid()
root.mainloop()
# 分別測試滑鼠的事件,回呼函數的參數event中(x,y)表示當前點擊的座標值
'''2.測試滑鼠的移動(Motion)事件'''
# -*- coding: cp936 -*-
# <Bx-Motion>:滑鼠移動事件,x=[1,2,3]分別表示左、中、右滑鼠操作。
from Tkinter import *
root = Tk()
def printCoords(event):
print event.x,event.y
# 建立第一個Button,並將它與左鍵移動事件綁定
bt1 = Button(root,text = 'leftmost button')
bt1.bind('<B1-Motion>',printCoords)
# 建立二個Button,並將它與中鍵移動事件綁定
bt2 = Button(root,text = 'middle button')
bt2.bind('<B2-Motion>',printCoords)
# 建立第三個Button,並將它與右擊移動事件綁定
bt3 = Button(root,text = 'rightmost button')
bt3.bind('<B3-Motion>',printCoords)
bt1.grid()
bt2.grid()
bt3.grid()
root.mainloop()
# 分別測試滑鼠的移動事件,只有當滑鼠被按下後移動才回產生事件
'''3.測試滑鼠的釋放(Relase)事件'''
# -*- coding: cp936 -*-
# <ButtonRelease-x>滑鼠釋放事件,x=[1,2,3],分別表示滑鼠的左、中、右鍵操作
from Tkinter import *
root = Tk()
def printCoords(event):
print event.x,event.y
# 建立第一個Button,並將它與左鍵釋放事件綁定
bt1 = Button(root,text = 'leftmost button')
bt1.bind('<ButtonRelease-1>',printCoords)
# 建立二個Button,並將它與中鍵釋放事件綁定
bt2 = Button(root,text = 'middle button')
bt2.bind('<ButtonRelease-2>',printCoords)
# 建立第三個Button,並將它與右擊釋放事件綁定
bt3 = Button(root,text = 'rightmost button')
bt3.bind('<ButtonRelease-3>',printCoords)
bt1.grid()
bt2.grid()
bt3.grid()
root.mainloop()
# 分別測試滑鼠的Relase事件,只有當滑鼠被Relase後移動才回產生Relase事件
'''4.進入(Enter)事件'''
# -*- coding: cp936 -*-
# <Enter>:滑鼠釋放事件
from Tkinter import *
root = Tk()
def printCoords(event):
print event.x,event.y
# 建立第一個Button,並將它與Enter事件綁定
bt1 = Button(root,text = 'leftmost button')
bt1.bind('<Enter>',printCoords)
bt1.grid()
root.mainloop()
# 分別測試Enter事件,只是在第一次進入進回產生事件,在組件中移動不會產生Enter事件。