This article introduces the python five pieces of chess how to get the mouse to click the coordinates of the text through the sample code is very detailed, on everyone's learning or work has a certain reference learning value, the need for friends can refer to the next!
The point coordinates are taken from:
from tkinter import * root=Tk() # Create a frame in which to respond to events frame=Frame(root,width=200,height=200) def callBackLeft(event): print("Relative to the position of the upper left corner of the application, the position of the left click is",,) print("Relative to the position of the upper left corner of the screen, the left click is",event.x_root,event.y_root) def callBackRight(event): print("The location of the right click is ",,) print("The location of the right click is ",event.x_root,event.y_root) ("<Button-1>",callBackLeft) ("<Button-3>",callBackRight) () mainloop()
The result is shown in the figure:
The coordinates are processed and filtered to obtain specific coordinates.
from tkinter import * root = Tk() size = 16 def piant(event): if % 30 > 15: = // 30 + 1 else: = // 30 if % 30 > 15: = // 30 + 1 else: = // 30 # Edge detection if > size: = size if > size: = size if < 1: = 1 if < 1: = 1 print("x-coordinate: %d,y-coordinate: %d"%(,)) canvas = Canvas(root, width=500, height=500) (expand=YES, fill=BOTH) ("<Button-1>",piant) () # Drawing vertical lines for num in range(1, 17): canvas.create_line(num * 30, 30, num * 30, 480, width=2) # Draw a line across for num in range(1, 17): canvas.create_line(30, num * 30, 480, num * 30, width=2) ()
The result is shown in the figure:
This is the whole content of this article.