我已经构建了一个练习龟库的程序,并要求用户选择循环中的哪个形状

时间:2017-05-02 17:09:08

标签: python turtle-graphics

import turtle as t
import turtle as t
from enum import Enum
def Create_star_with_inside_borders (pointer, screen ,BackgroundColor , penColor, color_mix, screenWith ,screenHight, central_point_x_value,central_point_y_value, length):
     angle = 144
     screen.title('Star')
     screen.setup(screenWith,screenHight)                        #choosing the screen size
     screen.bgcolor(BackgroundColor)                             #making canvas black
     pointer.penup()                                             #taking pen away
     pointer.pencolor(penColor)                                  #choosing the pen colour            
     pointer.pensize(10)                                         #choosing the size of pen nib 
     pointer.speed(1)                                            #choosing the speed of drawing
     pointer.shape('turtle')                                     #choosing the pointer shape
 #    pointer.home()                                              #reseting the orientation of the pointer
     #   without reseting everything
     pointer.goto(central_point_x_value,central_point_y_value)   #starting point 
     pointer.fillcolor(color_mix)                                #choosing fill shape color
     pointer.begin_fill()                                        #begining filling shape
     pointer.pendown()                                           #start typing
     for i in range(5):
         pointer.left(angle)
         pointer.forward(length)
         pointer.left(144/2)
         pointer.forward(length)
     pointer.end_fill()                                          #filling the shape 
     pointer.penup()                                             #taking pen away
     pointer.setpos(screenWith,screenHight)                      #goto x,y point
     pointer.ht()                                                #hide turtle
     t.done()                                               #finish drawing shape
for i in (5):   
    if (Shape(int(SelectedShape)).name == 'BorderStar'):
        turtleMe.Create_star_with_inside_borders(pointer,screen , BackGroundColor , penColor, ShapeFillingColor,  int(screenWith), int(screenHight), 0 , 0 , int(size))

我已经构建了一个练习龟库的程序,并要求用户选择循环中的哪个形状。但是,在关闭龟屏或写再见或exitonclick命令之前,我无法退出外循环。有没有什么可以在关闭它之后再次获得窗口以绘制另一个形状?

1 个答案:

答案 0 :(得分:0)

Python龟模块不愿意关闭并重新打开它的窗口。你可以做的是reset()窗口。这将为您提供一个新的空白画布,以便重绘您的海龟。这是以这种方式重新编写的示例代码:

import time
import random
from turtle import Turtle, Screen

screenWidth, screenHeight = 800, 600

def Create_star_with_inside_borders(pointer, screen, BackgroundColor, penColor, color_mix, screenWidth, screenHeight, central_point_x_value, central_point_y_value, length):
    angle = 144

    screen.title('Star')
    screen.setup(screenWidth, screenHeight)  # choosing the screen size
    screen.bgcolor(BackgroundColor)  # making canvas black

    pointer.pencolor(penColor)  # choosing the pen colour
    pointer.fillcolor(color_mix)  # choosing fill shape color
    pointer.pensize(10)  # choosing the size of pen nib

    pointer.penup()  # taking pen away
    pointer.goto(central_point_x_value, central_point_y_value)  # starting point
    pointer.pendown()  # start typing

    pointer.begin_fill()  # begin filling shape

    for _ in range(5):
        pointer.left(angle)
        pointer.forward(length)
        pointer.left(angle / 2)
        pointer.forward(length)

    pointer.end_fill()  # end filling the shape
    pointer.penup()  # taking pen away

pointer = Turtle('turtle')
screen = Screen()

for _ in range(5):
    BackGroundColor = random.choice(['pink', 'black', 'white'])
    penColor = random.choice(['red', 'green', 'blue'])
    ShapeFillingColor = random.choice(['magenta', 'cyan', 'yellow'])

    size = random.randint(50, 250)

    Create_star_with_inside_borders(pointer, screen, BackGroundColor, penColor, ShapeFillingColor, screenWidth, screenHeight, 0, 0, size)

    pointer.hideturtle()  # hide turtle
    time.sleep(5)
    screen.reset()

screen.bye()

它应该使用不同的设置运行您的绘图代码五次。