如何在鼠标点击后显示x坐标? - Python

0 投票
1 回答
760 浏览
提问于 2025-04-16 12:20

我正在想办法让用户在图形窗口点击某个点时,能够显示出那个点的x坐标。有没有什么好主意?谢谢!

这是我的代码:

设置图形窗口

win = GraphWin("Uncle Scrooges Money Bin", 640,480)
win.setCoords(0,0,80,60)
win.setBackground("white")

获取鼠标点击的坐标 1

point1 = win.getMouse() #*****************************

显示点 1 的坐标

print("Point 1 coordinates: ", point1)

1 个回答

0

库来做你说的事情会简单很多。

import Tkinter

def mouse(event):
    print "Point 1 coordinate :",event.x,event.y
# event parameter will be passed to the function when the mouse is clicked
# This parameter also contains co-ordinates of where the mouse was clicked 
# which can be accessed by event.x and event.y

win = Tkinter.Tk()  # Main top-level window

win.title("Uncle Scrooges Money Bin")  
win.geometry("640x480+80+60")  # Set window diensions

frame = Tkinter.Frame(win, background='white', width=640, height=480)  
# New frame to handle mouse-clicks

frame.pack()  # pack frame(or make frame visible)
frame.bind('<Button-1>', mouse)  
# Bind mouse-click event denoted by '<Button-1>' to mouse function 

win.mainloop()  # Start window main loop

撰写回答