如何在点击鼠标后显示x坐标? - Python
问题描述:
我想弄清楚如何让用户在图形窗口中单击一个点时显示x坐标。有任何想法吗?谢谢!如何在点击鼠标后显示x坐标? - Python
这里是我的代码:
设置图形窗口
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)
答
使用Tkinter
库会更容易。
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
如果您指出了用于窗口化的框架,以及您的代码出现了什么问题,会发生什么或不是什么情况? – 2011-02-22 20:47:58
似乎他可能会使用这个:http://mcsp.wartburg.edu/zelle/python/graphics/graphics/node1.html – jdi 2011-12-18 19:24:56
另外,它在最后一行中打印什么? – Robin 2011-12-18 19:31:07