Skip to main content
 首页 » 编程设计

python之Turtle 图形 begin_fill() 函数在 MAC 上无法正常工作

2024年11月24日8artech

我正在尝试使用模块 turtle 绘制一个“黄色”星星。当我在 Windows 操作系统上运行我的程序时,它可以正常工作。但是,当我在 macOS 上运行它时,图形是错误的。 Result on macOS

Result on Windows OS

import turtle 
# Setup a screen and a turtle 
win = turtle.Screen() 
bob = turtle.Turtle() 
# set the background color for the flag 
win.bgcolor("red") 
# Draw a star 
# change the turtle color to yellow  
bob.color("yellow") 
# to center we have to go backward for half of a side length 
bob.penup() 
bob.back(100) 
bob.pendown() 
bob.begin_fill() 
for i in range(5): 
    bob.forward(200) 
    bob.right(144) 
bob.end_fill() 
win.exitonclick() 

请您参考如下方法:

这不是 turtle 问题,而是底层 tkinter 库的问题。当涉及交叉线时,两个操作系统上的填充是不同的。解决方法是画星,不要交叉线:

from turtle import Screen, Turtle 
 
win = Screen() 
win.bgcolor("red") 
 
bob = Turtle() 
bob.color("yellow") 
 
bob.penup() 
bob.goto(24.5, 33.1) 
bob.pendown() 
 
bob.begin_fill() 
 
for i in range(5): 
    bob.forward(80) 
    bob.right(144) 
    bob.forward(80) 
    bob.left(72) 
 
bob.end_fill() 
bob.hideturtle() 
 
win.exitonclick() 

这在两种实现上看起来应该是一样的: