我已经构建了一个练习海龟库的程序,并要求用户在循环中选择哪个形状
I have build a program practicing turtle library and ask the user to choose which shape in a loop
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))
我建立了一个练习海龟库的程序,让用户循环选择哪个形状。但是,在关闭乌龟屏幕或写入 bye 或 exitonclick 命令之前,我无法退出外循环。有什么为什么在关闭它以绘制另一个形状后再次获得window?
Python turtle 模块不愿意关闭并重新打开它是 window。您可以做的是 reset()
window。这会给你一个新的空白 canvas 来绘制和重置你的海龟。这是您以这种方式修改的示例代码:
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()
它应该 运行 你的绘图代码五次不同的设置。
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))
我建立了一个练习海龟库的程序,让用户循环选择哪个形状。但是,在关闭乌龟屏幕或写入 bye 或 exitonclick 命令之前,我无法退出外循环。有什么为什么在关闭它以绘制另一个形状后再次获得window?
Python turtle 模块不愿意关闭并重新打开它是 window。您可以做的是 reset()
window。这会给你一个新的空白 canvas 来绘制和重置你的海龟。这是您以这种方式修改的示例代码:
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()
它应该 运行 你的绘图代码五次不同的设置。