简体   繁体   English

初学者尝试 Python Turtle 模块

[英]Beginner trying Python Turtle module

I'm a beginner just trying to mess around with Python.我是一个初学者,只是想弄乱Python。 I wrote some very simple code using the Turtle module, but something I can't figure out is why the GUI closes immediately after it's done drawing?我使用 Turtle 模块编写了一些非常简单的代码,但我无法弄清楚为什么 GUI 在完成绘图后立即关闭?

I've tried turtle.getscreen()._root.mainloop(), and the sleep command (which my cmd doesn't recognize), but to no avail.我已经尝试过 turtle.getscreen()._root.mainloop() 和 sleep 命令(我的 cmd 无法识别),但无济于事。 Any thoughts?有什么想法吗?

Realize this is a trivial question, but people say the best way to understand things is to get in there and do random things :)意识到这是一个微不足道的问题,但人们说理解事物的最佳方法是进入那里并做一些随机的事情:)

Code ( extracted from comment):代码(从评论中提取):

from turtle import *
setup()
title("turtle test")
clear()
down()
forward(50)
right(90)
forward(50)
right(90)
forward(50)
right(90)
forward(500)
turtle.getscreen()._root.mainloop()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'turtle' is not defined

Because you did from turtle import * you do not have a turtle module for turtle.getscreen()._root.mainloop() , generating the error above.因为你做了from turtle import *你没有turtle.getscreen()._root.mainloop()turtle模块,产生上面的错误。

Instead, try mainloop() .相反,请尝试mainloop()

The screen should not "disappear"- if you are calling the mainloop() method correctly - however, if there is a syntax error in your source code, or other Python exception is raised, the program would finish immediately.屏幕不应该“消失”——如果你正确地调用了 mainloop() 方法——但是,如果你的源代码中存在语法错误,或者引发了其他 Python 异常,程序将立即完成。

If instead of clicking on your program, you run it from a command terminal, you will see the error traceback.如果不是单击您的程序,而是从命令终端运行它,您将看到错误回溯。

POst it on your question (along with your code, properly formated, which you can do by clicking on "edit" on the question), so that people may help you further.将其发布在您的问题上(连同您的代码,格式正确,您可以通过单击问题上的“编辑”来完成),以便人们可以进一步帮助您。

(btw, calling the mainloop method in the way you describe is works for me). (顺便说一句,以您描述的方式调用 mainloop 方法对我有用)。

Now one thing: the built-in Python Tkinter turtle is mostly a toy, and the fun part is playing along with it in the interactive mode, typing commands to it as you go, not to write a script with it.现在有一件事:内置的 Python Tkinter 乌龟主要是一个玩具,有趣的部分是在交互模式下与它一起玩,边走边输入命令,而不是用它编写脚本。 If you want to do some serious art using a turtle model for driving, you be better writing your own turtle.如果您想使用海龟模型进行一些严肃的艺术驾驶,您最好编写自己的海龟。

像这样修复它

from turtle import *

setup()

title("turtle test")

clear()

down()

forward(50)

right(90)

forward(50)

right(90)

forward(50)

right(90)

forward(500)

done()

My answer would be to remove root because it's not assigned ,here is my code try it out:我的答案是删除 root,因为它没有被分配,这是我的代码尝试一下:

from turtle import *
setup()
title("turtle test")
clear()
down()
forward(50)
right(90)
forward(50)
right(90)
forward(50)
right(90)
forward(500)
mainloop()

Okay this is maybe too much but you can use a for loop(a loop that repeats for some amount of time):好的,这可能太多了,但是您可以使用 for 循环(重复一段时间的循环):

from turtle import *
setup()
title("turtle test")
clear()
down()
for i in range(3):
    forward(50)
    right(90)
forward(500)
mainloop()

Python is a great language consider working on learning it! Python 是一门很棒的语言,请考虑学习它!

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

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