File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ '''
2+ This example shows you how you can capture mouse click events in tkinter
3+ '''
4+ #import the library
15from tkinter import *
2-
6+ #create a window object
37root = Tk ()
4-
8+ #create the function to handle the click events
9+ #note that event is a special object in tkinter that will be generated each time there is a click
510def callback (event ):
11+ #note that we can do other things with our x and y now that we know how to get them
612 print ("clicked at" , event .x , event .y )
713
14+ #setup the window size
815frame = Frame (root , width = 400 , height = 400 )
16+ #everything in the window will be considered a button, which calls the callback function when pressed
917frame .bind ("<Button-1>" , callback )
18+ #put the sizing and button definition into our window (pack is the simplest method)
1019frame .pack ()
1120
21+ #title the overall window
22+ root .title ("Example of capturing a mouse click location." )
23+ #show the window - windows run in a loop listening for the window close button to break the loop
1224root .mainloop ()
You can’t perform that action at this time.
0 commit comments