'''Tkinter event (1 )'''
# How to Use Events
'''1. Test the mouse click event '''
#-*-Coding: cp936 -*-
# <Button-1>: Left click event
# <Button-2>: Click an event
# <Button-3>: Right-click event
# <Double-button-1>: double-click the event
# <Triple-button-1>: trigger event
From tkinter import *
Root = TK ()
Def printcoords (event ):
Print event. X, event. Y
# Create the first button and bind it to the left-click event
Bt1 = button (root, text = 'leftmost click ')
Bt1.bind ('<button-1>', printcoords)
# Create two buttons and bind them to the key event
Bt2 = button (root, text = 'midddle ')
Bt2.bind ('<button-2>', printcoords)
# Create the third button and bind it to the right-click event
Bt3 = button (root, text = 'rightmost click ')
Bt3.bind ('<button-3>', printcoords)
# Create the fourth button and bind it with the double-click event
Bt4 = button (root, text = 'double click ')
Bt4.bind ('<double-button-1>', printcoords)
# Create the fifth button and bind it to the three-Click Event
BT5 = button (root, text = 'triple click ')
Bt5.bind ('<triple-button-1>', printcoords)
Bt1.grid ()
Bt2.grid ()
Bt3.grid ()
Bt4.grid ()
Bt5.grid ()
Root. mainloop ()
# Test the mouse events respectively. In the callback function parameter event (x, y), it indicates the coordinate value of the current click.
'''2. Test the motion event '''
#-*-Coding: cp936 -*-
# <Bx-motion>: move the cursor. x = [, 3] indicates the left, center, and right mouse operations.
From tkinter import *
Root = TK ()
Def printcoords (event ):
Print event. X, event. Y
# Create the first button and bind it to the left-click Move event
Bt1 = button (root, text = 'leftmost click ')
Bt1.bind ('<B1-Motion>', printcoords)
# Create two buttons and bind them to the move event in the middle key.
Bt2 = button (root, text = 'midddle ')
Bt2.bind ('<B2-Motion>', printcoords)
# Create the third button and bind it to the right-click Move event
Bt3 = button (root, text = 'rightmost click ')
Bt3.bind ('<B3-Motion>', printcoords)
Bt1.grid ()
Bt2.grid ()
Bt3.grid ()
Root. mainloop ()
# Test the mouse movement events separately. Events are generated only when the mouse is moved after being pressed.
'''3. Test the mouse release event '''
#-*-Coding: cp936 -*-
# <Buttonrelease-x> mouse release event, x = [1, 2, 3], indicating the left, center, and right-click operations of the mouse
From tkinter import *
Root = TK ()
Def printcoords (event ):
Print event. X, event. Y
# Create the first button and bind it to the left-click release event
Bt1 = button (root, text = 'leftmost click ')
Bt1.bind ('<ButtonRelease-1>', printcoords)
# Create two buttons and bind them to the release event of the middle key.
Bt2 = button (root, text = 'midddle ')
Bt2.bind ('<ButtonRelease-2>', printcoords)
# Create the third button and bind it to the release event by right-clicking
Bt3 = button (root, text = 'rightmost click ')
Bt3.bind ('<ButtonRelease-3>', printcoords)
Bt1.grid ()
Bt2.grid ()
Bt3.grid ()
Root. mainloop ()
# Test the relase events of the mouse separately. A relase event is generated only when the mouse is moved by relase.
'''4. Enter the (enter) event '''
#-*-Coding: cp936 -*-
# <Enter>: Mouse release event
From tkinter import *
Root = TK ()
Def printcoords (event ):
Print event. X, event. Y
# Create the first button and bind it to the enter event
Bt1 = button (root, text = 'leftmost click ')
Bt1.bind ('<enter>', printcoords)
Bt1.grid ()
Root. mainloop ()
# Test the enter event separately. The event is generated only after the first entry and return, and the enter event is not generated in the widget.