python主循环方法mainloop_Python turtle.mainloop方法代碼示例

# 需要導入模塊: 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

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值