隐藏龟窗? [英] Hide Turtle Window?

查看:32
本文介绍了隐藏龟窗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 Turtle 中生成图表,作为程序的一部分,我从图表中识别出某些坐标.我希望能够隐藏完整的海龟窗口,因为我只关心坐标,这可能吗?

I am generating diagrams in Turtle, and as part of my program, I identify certain coordinates from my diagrams. I would like to be able to hide the complete turtle window, since I only care about the coordinates, is that possible?

问题 2:

这不是真正的答案,而是一些其他问题.

This isn't really an answer, but a few other questions.

我的程序在某种程度上可以运行,如果您在 IDLE 中运行它并输入l",它将为您提供带有坐标的列表.

I got my program working to some extent, if you run it in IDLE and type "l" it will give you the list with the coordinates.

import Tkinter
import turtle

from turtle import rt, lt, fd   # Right, Left, Forward

size = 10

root = Tkinter.Tk()
root.withdraw()

c = Tkinter.Canvas(master = root)
t = turtle.RawTurtle(c)

t.speed("Fastest")

# List entire coordinates
l = []

def findAndStoreCoords():
    x = t.xcor()
    y = t.ycor()

    x = round(x, 0)     # Round x to the nearest integer
    y = round(y, 0)     # Round y to the nearest integer

    # Integrate coordinates into sub-list
    l.append([x, y])

def hilbert(level, angle):
    if level == 0:
        return

    t.rt(angle)
    hilbert(level - 1, -angle)
    t.fd(size)
    findAndStoreCoords()
    t.lt(angle)
    hilbert(level - 1, angle)
    t.fd(size)
    findAndStoreCoords()
    hilbert(level - 1, angle)
    t.lt(angle)
    t.fd(size)
    findAndStoreCoords()
    hilbert(level - 1, -angle)
    t.rt(angle)

问题是海龟太慢了!有没有像 Turtle 一样但可以更快地执行命令的包?

The problem is that Turtle is so SLOW! Is there any package that is just like Turtle but can do commands much faster?

推荐答案

我按照三十七的建议重新实现了海龟类.与api一致.(即当你在这个类中右转时,它与turtle中的右转相同.

I reimplemented the turtle class as suggested by thirtyseven. It is consistent with the api. (i.e. when you turn right in this class, it is the same as turning right in turtle.

这并没有实现api中的所有方法,只实现了一些常用的方法.(以及你使用的那些).

但是,它的扩展很短而且相当简单.此外,它会跟踪它已经到达的所有点.它通过在每次调用 forward、backward 或 setpos(或这些函数的任何别名)时向 pointsVisited 添加一个条目来实现这一点.

However, it's short and fairly straightforward to extend. Also, it keeps track of all of the points it has been to. It does this by adding an entry to pointsVisited every time you call forward, backward, or setpos (or any of the aliases for those functions).

import math

class UndrawnTurtle():
    def __init__(self):
        self.x, self.y, self.angle = 0.0, 0.0, 0.0
        self.pointsVisited = []
        self._visit()

    def position(self):
        return self.x, self.y

    def xcor(self):
        return self.x

    def ycor(self):
        return self.y

    def forward(self, distance):
        angle_radians = math.radians(self.angle)

        self.x += math.cos(angle_radians) * distance
        self.y += math.sin(angle_radians) * distance

        self._visit()

    def backward(self, distance):
        self.forward(-distance)

    def right(self, angle):
        self.angle -= angle

    def left(self, angle):
        self.angle += angle

    def setpos(self, x, y = None):
        """Can be passed either a tuple or two numbers."""
        if y == None:
            self.x = x[0]
            self.y = y[1]
        else:
            self.x = x
            self.y = y
        self._visit()

    def _visit(self):
        """Add point to the list of points gone to by the turtle."""
        self.pointsVisited.append(self.position())

    # Now for some aliases. Everything that's implemented in this class
    # should be aliased the same way as the actual api.
    fd = forward
    bk = backward
    back = backward
    rt = right
    lt = left
    setposition = setpos
    goto = setpos
    pos = position

ut = UndrawnTurtle()

这篇关于隐藏龟窗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆