简体   繁体   English

Python海龟图形使用文本文件

[英]Python turtle graphics using text file

enter image description here I need help with Turtle graphics.在此处输入图像描述我需要有关 Turtle 图形的帮助。 I want the script to read my text file, and then draw a figure using the parameters in the text file.我希望脚本读取我的文本文件,然后使用文本文件中的参数绘制图形。 I think I almost got it, but gett error messages in the last part of the code.我想我几乎明白了,但在代码的最后一部分得到错误消息。 Can somebody please help me, thank you: :)有人可以帮我吗,谢谢::)

import turtle
turtle.pensize(4)
turtle.hideturtle()
turtle.speed(5)

with open ("tenge_eksempel_2.txt" , "r") as tekst:
lest_tekst = tekst.readlines()
Antall = 0
List_lengde = len(lest_tekst)
while List_lengde > Antall:
        if List_lengde < Antall:
        turtle.done()
    try:
        verdi = int(lest_tekst[Antall])
        if verdi < 0:
            turtle.penup()
        turtle.penup()
        turtle.right(verdi)
        Antall = Antall+1
        verdi = int(lest_tekst[Antall])
        turtle.pendown()
        if verdi < 0:
            turtle.penup()
        turtle.forward(verdi)
        Antall = Antall+1
        turtle.pendown()

turtle done

my text file looks like this: black 15 200 lgihtgrey 150 200 etc...我的文本文件如下所示:黑色 15 200 lgihtgrey 150 200 等...

Given the text above: black 15 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200. Assuming the black and gray are turtle pen colors and the 1st number angle and the 2nd pen motion, How would you separate the color(string) from the number?给定上面的文字:黑色15 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200。假设黑色和灰色分别是龟角笔colors和第1号笔的动感您如何将颜色(字符串)与数字分开? I tried the above code from the accepted answer but got no drawing in python turtle graphics.我从接受的答案中尝试了上述代码,但在 python 海龟图形中没有绘图。 Just an empty blank turtle window.只是一个空的空白乌龟 window。

Edit: I got no error messages, nor do I get a message saying process completed.编辑:我没有收到错误消息,也没有收到说明过程已完成的消息。 I even left it to run for like 15-20 minutes just in case it was just slow.我什至让它运行了 15-20 分钟,以防万一它很慢。

Try this and see if it helps you figure out what's going on.试试这个,看看它是否能帮助你弄清楚发生了什么。

import turtle
turtle.pensize(4)
turtle.hideturtle()
turtle.speed(5)

with open ("tenge_eksempel_2.txt" , "r") as tekst:
    temp = tekst.readlines()
lest_tekst = temp [0].split ()

Antall = 1
List_lengde = len(lest_tekst)
while List_lengde > Antall:
    verdi = int(lest_tekst[Antall])
    if verdi > 0:
        turtle.right(verdi)
        Antall = Antall+1
        verdi = int(lest_tekst[Antall])
        print (verdi)
    if verdi > 0:
        turtle.forward(verdi)
    Antall = Antall+2

turtle.done ()

tenge_eksempel_2.txt is: tenge_eksempel_2.txt 是:

black 15 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200 black 300 200 lightgray 150 200 

Edit: On my system I get:编辑:在我的系统上,我得到: 在此处输入图像描述

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

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