Mouse events example. : Mouse « Tkinker « Python Tutorial






Mouse events example.
from Tkinter import *

class MouseLocation( Frame ):
   def __init__( self ):
      Frame.__init__( self )
      self.pack( expand = YES, fill = BOTH )
      self.master.title( "Demonstrating Mouse Events" )
      self.master.geometry(  "275x100" )
      
      self.mousePosition = StringVar() # displays mouse position
      self.mousePosition.set( "Mouse outside window" )
      self.positionLabel = Label( self,textvariable = self.mousePosition )
      self.positionLabel.pack( side = BOTTOM )

      self.bind( "<Button-1>", self.buttonPressed )
      self.bind( "<ButtonRelease-1>", self.buttonReleased )   
      self.bind( "<Enter>", self.enteredWindow )
      self.bind( "<Leave>", self.exitedWindow )
      self.bind( "<B1-Motion>", self.mouseDragged )

   def buttonPressed( self, event ):
      self.mousePosition.set( "Pressed at [ " + str( event.x ) + ", " + str( event.y ) + " ]" )

   def buttonReleased( self, event ):
      self.mousePosition.set( "Released at [ " + str( event.x ) + ", " + str( event.y ) + " ]" )

   def enteredWindow( self, event ):
      self.mousePosition.set( "Mouse in window" )

   def exitedWindow( self, event ):
      self.mousePosition.set( "Mouse outside window" )

   def mouseDragged( self, event ):
      self.mousePosition.set( "Dragged at [ " + str( event.x ) + ", " + str( event.y ) + " ]" )

MouseLocation().mainloop()








18.22.Mouse
18.22.1.Left button clickedLeft button clicked
18.22.2.Right button clickedRight button clicked
18.22.3.Drag with Left buttonDrag with Left button
18.22.4.Middle button clickedMiddle button clicked
18.22.5.Double left clickedDouble left clicked
18.22.6.Mouse events example.Mouse events example.
18.22.7.Mouse button differentiation.Mouse button differentiation.
18.22.8.Bind mouse button with action functionBind mouse button with action function