如何在不使用 turtle.mainloop() 的情况下停止海龟冻结?

当我要求用户输入海龟时,海龟页面冻结。

我试过使用turtle.mainloop(),但这只会完全停止程序。

有没有一种方法可以防止海龟页面冻结和程序停止?


这是代码:


import turtle  

import time 

import random

def quickshape(sides,size,thickness):

    int(thickness)

    turtle.width(thickness)

    int(sides)

    int(size)

    angle = 360 / sides

    int(angle)

    for i in range(sides):

        turtle.forward(size)

        turtle.right(angle)

def correct_position():

    global x

    global y

    turtle.penup()

    x = random.randint(-400,400)

    y = random.randint(-250,250)

    turtle.setpos(x, y)

def move_alot():

    global x

    global y

    turtle.penup()

    x += random.randint(-100,100)

    y += random.randint(-100,100)

goes = 1

print("My Doodle .co can draw you lots of shapes, depending on what you ask us!")

print("To make a new set of patterns, close the pattern page... but only when it is drawing.")

print()

print("Before we start, do you want day or night mode?")

while True:

    mode = input("Mode: ")

    if mode == 'day' or mode[0] == 'd':

        print("Day mode it is.")

        mode = 'day'

        break

    elif mode == 'night' or mode[0] == 'n':

        print("Night mode it is.")

        mode = 'night'

        break

    else:

        print("Hmm... which mode?")

titlename = input("Title: ")

print("Configuring - takes a couple of seconds...")

while True:

    turtle.ht()

    turtle.tracer(0)

    turtle.screensize()

    turtle.setup(width = 1.0, height = 1.0)

    turtle.title(titlename + " [waiting for input - do not close]")

    print("[ pattern",goes,"]")

    while True:

            try:

                sides = int(input("Sides: "))

            except ValueError:

                print("[Input Error]")

            else:

                if sides > 35:

                    print("Warning: >35 sides")

                break

    try:

        turtle.penup()

        x = random.randint(-200,200)

        y = random.randint(-100,100)

        turtle.setpos(x, y)


红糖糍粑
浏览 126回答 1
1回答

浮云间

这是代码 - 但它对问题来说不太重要。代码对问题总是很重要!有足够的代码,而且问题相当模糊,我将尝试将您的代码翻译成 Python 的方法;-),看看您的问题是否仍然存在:from turtle import Screen, Turtlefrom random import random, randintdef quickshape(sides, size, thickness):&nbsp; &nbsp; turtle.width(thickness)&nbsp; &nbsp; angle = 360 / sides&nbsp; &nbsp; for _ in range(sides):&nbsp; &nbsp; &nbsp; &nbsp; turtle.forward(size)&nbsp; &nbsp; &nbsp; &nbsp; turtle.right(angle)def correct_position():&nbsp; &nbsp; global x, y&nbsp; &nbsp; x = randint(-400, 400)&nbsp; &nbsp; y = randint(-250, 250)def move_alot():&nbsp; &nbsp; global x, y&nbsp; &nbsp; x += randint(-100, 100)&nbsp; &nbsp; y += randint(-100, 100)print("My Doodle .co can draw you lots of shapes, depending on what you ask us!")print()print("Before we start, do you want day or night mode?")goes = 1while True:&nbsp; &nbsp; mode = input("Mode: ")&nbsp; &nbsp; if mode[0].lower() == 'd':&nbsp; &nbsp; &nbsp; &nbsp; print("Day mode it is.")&nbsp; &nbsp; &nbsp; &nbsp; mode = 'day'&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; if mode[0].lower() == 'n':&nbsp; &nbsp; &nbsp; &nbsp; print("Night mode it is.")&nbsp; &nbsp; &nbsp; &nbsp; mode = 'night'&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; print("Hmm... which mode?")titlename = input("Title: ")print("Configuring - takes a couple of seconds...")screen = Screen()screen.tracer(False)screen.setup(width=1.0, height=1.0)if mode == 'day':&nbsp; &nbsp; screen.bgcolor("white")else:&nbsp; &nbsp; screen.bgcolor("black")turtle = Turtle()turtle.hideturtle()while True:&nbsp; &nbsp; screen.title(titlename + " [Waiting for input - do not close]")&nbsp; &nbsp; pattern = "[Pattern {}]".format(goes)&nbsp; &nbsp; sides = screen.numinput(pattern, "Number of sides:", default=6, minval=3, maxval=35)&nbsp; &nbsp; if sides is None:&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; sides = int(sides)&nbsp; # numinput() returns float&nbsp; &nbsp; numshapes = screen.numinput(pattern, "Number of shapes:", default=3, minval=1, maxval=50)&nbsp; &nbsp; if numshapes is None:&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; numshapes = int(numshapes)&nbsp; &nbsp; size = screen.numinput(pattern, "Length of each side:", default=25, minval=5, maxval=500)&nbsp; &nbsp; if size is None:&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; thickness = screen.numinput(pattern, "Thickness of pen:", default=1, minval=1, maxval=10)&nbsp; &nbsp; if thickness is None:&nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; x = randint(-200, 200)&nbsp; &nbsp; y = randint(-100, 100)&nbsp; &nbsp; for part in range(1, numshapes + 1):&nbsp; &nbsp; &nbsp; &nbsp; turtle.penup()&nbsp; &nbsp; &nbsp; &nbsp; percentage = round(part/numshapes * 100, 2)&nbsp; &nbsp; &nbsp; &nbsp; screen.title(titlename + " [Drawing pattern. " + str(percentage) + "% complete.]")&nbsp; &nbsp; &nbsp; &nbsp; turtle.color(random(), random(), random())&nbsp; &nbsp; &nbsp; &nbsp; if randint(1, 45) == 1:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; move_alot()&nbsp; &nbsp; &nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; x += randint(-10, 5)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; y += randint(-5, 10)&nbsp; &nbsp; &nbsp; &nbsp; if not (-650 < x < 650 and -500 < y < 500):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; correct_position()&nbsp; &nbsp; &nbsp; &nbsp; turtle.setposition(x, y)&nbsp; &nbsp; &nbsp; &nbsp; turtle.pendown()&nbsp; &nbsp; &nbsp; &nbsp; quickshape(sides, size, thickness)&nbsp; &nbsp; &nbsp; &nbsp; screen.title(titlename + " [Finished drawing pattern " + str(goes) + "]")&nbsp; &nbsp; &nbsp; &nbsp; screen.update()&nbsp; &nbsp; goes += 1screen.mainloop()我遗漏了以下功能:要制作一组新图案,请关闭图案页面......但仅限于绘制时。因为一旦关闭窗口,就无法再次启动 Python turtle。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python