# 需要導入模塊: import turtle [as 別名]
# 或者: from turtle import mainloop [as 別名]
def main():
# use sys.argv if needed
print('genterating spirograph...')
# create parser
descStr ="""This program draws spirographs using the Turtle module.
When run with no arguments, this program draws random spirographs.
Terminology:
R: radius of outer circle.
r: radius of inner circle.
l: ratio of hole distance to r."""
parser = argparse.ArgumentParser(description=descStr) # 創建參數解析器對象
parser.add_argument('--sparams',nargs=3,dest='sparams',required=False,
help="The three arguments in sparams:R,r,l.") # 向解析器添加可選參數
# parse args
args=parser.parse_args() # 調用函數進行實際的解析
# set to 80% screen width
turtle.setup(width=0.8)
# set cursor shape
turtle.shape('turtle')
# set title
turtle.title("Spirographs!")
# add key handler for saving image
turtle.onkey(saveDrawing,"s")
# start listening
turtle.listen()
# hide main turtle cursor
turtle.hideturtle()
# checks args and draw # 首先檢查是否有參數賦給--sparams.如果有,就從字符串中提取他們,用“列表解析”將他們轉換成浮點數
if args.sparams:
params=[float(x) for x in args.sparams]
# draw spirograph with given parameters
# black by default
col=(0.0,0.0,0.0)
spiro=Spiro(0,0,col,*params)
spiro.draw()
else:
# creat animator object
spiroAnim=SpiroAnimator(4)
# add key handler to toggle turtle cursor
turtle.onkey(spiroAnim.toggleTurtles,"t")
# add key handler to toggle turtle cursor
turtle.onkey(spiroAnim.restart,"space")
# start turtle main loop
turtle.mainloop()
# call main