简体   繁体   English

使用Zelle graphics.py在Python中进行getMouse跟踪

[英]getMouse tracking in Python using Zelle graphics.py

I am new to Python. 我是Python的新手。 I need to write a program to move my ball or circle when I click the mouse. 单击鼠标时,我需要编写一个程序来移动球或圆。 How do I achieve this? 我该如何实现这一目标? I have the below code that I got started with: 我有以下开始的代码:

from graphics import *
import time

def MouseTracker():

win = GraphWin("MyWindow", 500, 500)
win.setBackground("blue")
cir = Circle(Point(250,250) ,20)
cir.setFill("red")
cir.draw(win)

while(win.getMouse() != None):
    xincr = 0
    yincr = 0
for i in range(7):
    cir.move(xincr, yincr)
    time.sleep(.2)
win.getMouse()

Assuming you are not bound to some specific tools or implementation, you may find matplotlib useful. 假设您不受某些特定工具或实现的约束,那么您可能会发现matplotlib有用。 You can plot a circle onto the drawing area using a circle patch ( http://matplotlib.org/api/patches_api.html ) and then move it around when there is mouse-click in the graph axes. 您可以使用圆形补丁( http://matplotlib.org/api/patches_api.html )在绘图区域上绘制圆形,然后在图形轴上单击鼠标时将其移动。 You will need to connect to the event-click listener and define a callback function which handles the drawing update - see http://matplotlib.org/users/event_handling.html for examples of how to do this. 您将需要连接到事件单击侦听器,并定义一个用于处理图形更新的回调函数-有关如何执行此操作的示例,请参见http://matplotlib.org/users/event_handling.html You can get the coordinates of the mouse press using the xdata and ydata methods. 您可以使用xdata和ydata方法获取鼠标按下的坐标。

This worked for me in python 2.7: 这在python 2.7中对我有用:

import matplotlib.pyplot as plt
from matplotlib.patches import Circle

fig = plt.figure()
ax = fig.add_subplot(111)
circ = Circle((0.5,0.5), 0.1)
ax.add_patch(circ)

def update_circle(event):
    ax.cla()
    circ = Circle((event.xdata, event.ydata), 0.1)
    ax.add_patch(circ)
    fig.canvas.draw()

fig.canvas.mpl_connect('button_press_event', update_circle)
plt.show()

Assuming you want to stick with the graphics package you started with, you can do it but you're missing code to save the mouse position and compare it to the circle's center position: 假设您要坚持使用最初的图形包,可以这样做,但是缺少保存鼠标位置并将其与圆的中心位置进行比较的代码:

from graphics import *

WIDTH, HEIGHT = 500, 500
POSITION = Point(250, 250)
RADIUS = 20
STEPS = 7

def MouseTracker(window, shape):
    while True:
        position = window.getMouse()

        if position != None:  # in case we want to use checkMouse() later
            center = shape.getCenter()
            xincr = (position.getX() - center.getX()) / STEPS
            yincr = (position.getY() - center.getY()) / STEPS
            for _ in range(STEPS):
                shape.move(xincr, yincr)

win = GraphWin("MyWindow", WIDTH, HEIGHT)
win.setBackground("blue")

cir = Circle(POSITION, RADIUS)
cir.setFill("red")
cir.draw(win)

MouseTracker(win, cir)

You will need to close the window to break out of the tracking loop -- in a real program you'd handle this as part of the design (ie some user action causes a break in the while True: loop.) 您将需要关闭窗口以脱离跟踪循环-在实际程序中,您将在设计过程中处理此问题(即,某些用户操作会导致while True:循环break 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM