本文介绍了是否可以“隐藏"或禁用海龟屏幕并仅捕获最终图形?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

查看 Turtle 对象绘制的画布窗口有多大必要?可以隐藏或禁用它并仅捕获最终图形吗?

How necessary is viewing the canvas window that a Turtle object draws on? Can it be hidden or disable and just the final graphics captured?

我有一个使用 turtle 库的 Python 脚本,我想知道在脚本运行期间打开画布窗口(可以在其中查看正在创建的实际绘图)是否至关重要?是否有可能甚至不加载画布的可视部分并在最后简单地捕获数据?(顺便说一句,我创建了一个 .ps 文件并使用 PIL 将其转换为 .jpg).

I have a python script that uses the turtle library and am wondering is it crucial for the canvas window, where one can view the actual drawing being created, to be open during the script's runtime? Is it possible to not even load the visual portion of the canvas and simply capture the data at the end? (which btw I create a .ps file with and use PIL to convert it to a .jpg).

这是否会使整个脚本运行得更快,因为我计划显着增加我的迭代次数,而且它已经相当慢了(这当然可能是库的性质,我必须处理它)?

Would this perhaps make the overall script run faster as I am planning on bumping up my iteration count significantly and it is already fairly slow as is (which could of course be the nature of the library and I have to just deal with it)?

在 Google 上搜索后,我能找到的唯一问题是诸如隐藏海龟画布"之类的术语.或禁用画布窗口"刚刚解决了与我的问题无关的问题.我还搜索了 turtle 库文档,但似乎没有任何函数或方法可以完成这项工作,但我当然可能会错过它.

Having searched Google, the only issues I could find with terms like, "hide turtle canvas" or "disable canvas window" just netted me issues unrelated to my question. I have also searched through the turtle library documentation and no function or methods seemed to do the job, but of course I could have missed it.

我对根包 tkinter 不熟悉,所以如果在那端有什么我可能会弄乱的东西,我可以将其视为解决方案.但是我不知道从哪里开始,因为我没有完全理解 tkinterturtle 之间的关系.

I'm not familiar with the root package tkinter so if there is something on that end that I could mess with I can look into that as a solution. But I wouldn't know where to begin as I don't fully comprehend the relationship between tkinter and turtle.

推荐答案

使用 embedded 海龟,我们可以立即创建和撤回 Tk 根窗口,然后只需使用 Canvas:

Using embedded turtle, we can create and withdraw the Tk root window immediately and then simply work with a Canvas:

from tkinter import *
from turtle import RawTurtle

(root := Tk()).withdraw()

canvas = Canvas(root)
turtle = RawTurtle(canvas)
turtle.speed('fastest')

turtle.begin_poly()

for _ in range(5):
    turtle.forward(100)
    turtle.right(360/5)

turtle.end_poly()

print(turtle.get_poly())

输出

> python3 test.py
((0.00,0.00), (100.00,0.00), (130.90,-95.11), (50.00,-153.88), (-30.90,-95.11), (0.00,0.00))
>

我有意避免使用 TurtleScreenScrolledCanvas 包装类,因为我们不需要任何交互方法.

I'm intentionally avoiding the TurtleScreen and ScrolledCanvas wrapper classes as we don't need any interactive methods.

通过 getcanvas() 挖掘基础并找到要退出的根窗口,从 standalone 海龟可能可以完成同样的事情.

The same thing could probably be done from standalone turtle by digging into the underpinnings via getcanvas() and finding the root window to withdraw.

不完美,但在我的系统上我看不到任何窗口.如果您看到一闪一闪,请查找 tkinter overrideredirect(1) 方法,在 withdraw() 之前调用它以避免闪现.

Not perfect, but on my system I don't see any window. If you see a flash of one, look up the tkinter overrideredirect(1) method, calling it just before the withdraw() to avoid the flash.

这篇关于是否可以“隐藏"或禁用海龟屏幕并仅捕获最终图形?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 04:17
查看更多