Tkinter教程之Event篇(1)’

來源:互聯網
上載者:User
'''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事件。

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.