python turtle 奇怪的光标跳转

标签 python turtle-graphics

我正在尝试使用鼠标绘制 turtle ,下面的演示代码可以正常工作,但有时在鼠标移动期间光标会跳转:

#!/usr/bin/env python
import turtle
import sys

width = 600
height = 300
def gothere(event):
    turtle.penup()
    x = event.x
    y = event.y
    print "gothere (%d,%d)"%(x,y)
    turtle.goto(x,y)
    turtle.pendown()

def movearound(event):
    x = event.x
    y = event.y
    print "movearound (%d,%d)"%(x,y)
    turtle.goto(x,y)

def release(event):
    print "release"
    turtle.penup()

def circle(x,y,r):
    turtle.pendown() 
    turtle.goto(x,y)
    turtle.circle(r)
    turtle.penup()
    return

def reset(event):
    print "reset"
    turtle.clear()

#------------------------------------------------#
sys.setrecursionlimit(90000)
turtle.screensize(canvwidth=width, canvheight=height, bg=None)
turtle.reset()
turtle.speed(0)
turtle.setup(width, height)

canvas = turtle.getcanvas()

canvas.bind("<Button-1>", gothere)
canvas.bind("<B1-Motion>", movearound)
canvas.bind("<ButtonRelease-1>", release)
canvas.bind("<Escape>",reset)

screen = turtle.Screen()
screen.setworldcoordinates(0,height,width,0)
screen.listen()

turtle.mainloop()
#------------------------------------------------#

请参阅下面的 gif 以了解实际行为:

enter image description here

不确定是否有任何 API 调用错误!

最佳答案

我发现您的代码有几个问题:

  • 您将 turtle 的面向对象接口(interface)与 该模块的功能接口(interface)。我推荐一个或 其他,但不是两者。查看我的 import 更改以强制仅使用 OOP。

  • 您正在使用低级别的 tkinter 鼠标和按键事件,而不是 turtle 自己的事件。我建议你尝试在 turtle 级别工作 (尽管与您的实现相比这引入了一个小故障,请参阅 下面。)

  • 您通过不关闭事件引入了意外的递归 在你的事件处理程序中。在这些处理程序中禁用事件 这会花费大量时间来清理您的图形。

这是我按照上述几行对您的代码进行的修改。一个小问题是,与您原来的不同,“将 turtle 移动到这里”和“开始拖动”将需要两次点击,一次屏幕点击将 turtle 移动到当前位置,一次点击 turtle 开始拖动。这是由于 turtle 提供给 tkinter 事件的接口(interface)不同。 (Python 3 在这方面稍微好一些,但不是针对这种情况。)

为了缓解这个问题,我使用了一个更大的 turtle 光标。我还添加了标题逻辑:

from turtle import Turtle, Screen, mainloop

WIDTH = 600
HEIGHT = 300

def gothere(x, y):
    screen.onscreenclick(gothere)  # disable events inside handler

    turtle.penup()
    print("gothere (%d,%d)" % (x, y))
    turtle.goto(x, y)
    turtle.pendown()

    screen.onscreenclick(gothere)

def movearound(x, y):
    turtle.ondrag(None)  # disable events inside handler

    turtle.setheading(turtle.towards(x, y))
    print("movearound (%d,%d)" % (x, y))
    turtle.goto(x, y)

    turtle.ondrag(movearound)

def release(x, y):
    print("release (%d,%d)" % (x, y))
    turtle.penup()

def reset():
    print("reset")
    turtle.clear()

screen = Screen()
screen.setup(WIDTH, HEIGHT)
# screen.setworldcoordinates(0, HEIGHT, WIDTH, 0)  # should work fine either way

turtle = Turtle('turtle')
turtle.speed('fastest')

turtle.ondrag(movearound)
turtle.onrelease(release)

screen.onscreenclick(gothere)
screen.onkey(reset, "Escape")

screen.listen()

mainloop()  # normally screen.mainloop() but not in Python 2

但也见this answer我在这里展示了如何使 tkinter 的 onmove 事件对 turtle 可用。

... the "move here" then "start drag" limitation is very not comfortable for user? How can we improve that?

将我上面的代码与我链接到的替代答案结合起来,我们得到了这个解决方案,它与您开始的地方类似,但没有任何问题,而且风格更像 turtle :

from turtle import Turtle, Screen, mainloop
from functools import partial

WIDTH = 600
HEIGHT = 300

VERBOSE = False

def onscreenmove(self, fun, btn=1, add=None):  # method missing from turtle.py

    if fun is None:
        self.cv.unbind('<Button%s-Motion>' % btn)
    else:
        def eventfun(event):
            fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)

        self.cv.bind('<Button%s-Motion>' % btn, eventfun, add)

def onscreenrelease(self, fun, btn=1, add=None):  # method missing from turtle.py

    if fun is None:
        self.cv.unbind("<Button%s-ButtonRelease>" % btn)
    else:
        def eventfun(event):
            fun(self.cv.canvasx(event.x) / self.xscale, -self.cv.canvasy(event.y) / self.yscale)

        self.cv.bind("<Button%s-ButtonRelease>" % btn, eventfun, add)

def gothere(x, y):

    if VERBOSE:
        print("gothere (%d,%d)" % (x, y))

    turtle.penup()
    turtle.goto(x, y)
    turtle.pendown()

def movearound(x, y):

    screen.onscreenmove(None)  # disable events inside handler

    if VERBOSE:
        print("movearound (%d,%d)" % (x, y))


    turtle.setheading(turtle.towards(x, y))
    turtle.goto(x, y)

    screen.onscreenmove(movearound)  # reenable events

def release(x, y):

    if VERBOSE:
        print("release (%d,%d)" % (x, y))

    turtle.penup()

def reset():

    if VERBOSE:
        print("reset")

    turtle.clear()

screen = Screen()
screen.setup(WIDTH, HEIGHT)
screen.onscreenrelease = partial(onscreenrelease, screen)  # install missing methods
screen.onscreenmove = partial(onscreenmove, screen)

turtle = Turtle('turtle')
turtle.speed('fastest')

screen.onscreenclick(gothere)
screen.onscreenrelease(release)
screen.onscreenmove(movearound)

screen.onkey(reset, "Escape")
screen.listen()

mainloop()  # normally screen.mainloop() but not in Python 2

关于 python turtle 奇怪的光标跳转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50105860/

相关文章:

python - 无效的 float 。消息错误Python

Python3 导入错误 : No module named '_tkinter'

python - lxml.etree.iterparse 关闭输入文件处理程序?

python - Selenium Python 如何从 <div> 获取文本(html 源)

Python Turtle 比较颜色

python - 使用随机函数(Python turtle 图形)

python - 在Python Bluemix服务器中写入图像

python - Python 如何计算这个表达式?

python - 适用于多只 turtle 的功能

python - 使按键绑定(bind)适用于《太空侵略者》游戏