1

试图让鼠标指针的坐标显示在鼠标所在的画布上。这是代码。我可以让文本显示只是找不到显示鼠标本身坐标的技巧。任何帮助,将不胜感激。

from tkinter import *                
width = 250
height = 250
class MainGUI:
    def __init__(self):
        window = Tk() 
        window.title("Display Cursor Position")
        self.canvas = Canvas(window, bg = "white", width = width, height = height)
        self.canvas.pack()
        self.canvas.bind("<Button-1>", self.processMouseEvent)
        self.canvas.focus_set()
        window.mainloop()
    def processMouseEvent(self, event):
        self.canvas.create_text(event.x, event.y, text = "event.x, event.y")
        #self.canvas.insert(cursorPoint)
MainGUI()
4

1 回答 1

3

在这一行

self.canvas.create_text(event.x, event.y, text = "event.x, event.y")

前两个参数告诉您文本在画布中的位置。当您要插入鼠标坐标时,您必须将 event.x 和 event.y 转换为字符串(它们是整数)。所以:

def processMouseEvent(self, event):
    mouse_coordinates= str(event.x) + ", " + str(event.y)
    self.canvas.create_text(event.x, event.y, text = mouse_coordinates)
于 2013-10-24T17:30:53.463 回答