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、付费专栏及课程。

余额充值