简体   繁体   English

在Python窗口中重复功能

[英]Repeat function in window in Python

How do I repeat this turtle function in the window for a certain period of time? 如何在一段时间内在窗口中重复执行此乌龟功能? This code draws squares in a circular shape and I want it to restart once it is done and to do this for 3min and 30sec. 这段代码将正方形绘制成圆形,我希望它在完成后重新启动,并执行3分钟和30秒。

import turtle
import threading



def draw_square(some_shape):
    for i in range (1,5):
        some.forward(100)
        some.right(90)

def draw_art():
    window = turtle.Screen()
    window.bgcolor("yellow")
    sqr = turtle.Turtle()
    sqr.shape("triangle")
    sqr.color("purple")
    sqr.speed(1.5)
    for i in range(1,37):
        draw_square(sqr)
        sqr.right(10)
    window.exitonclick()

def timed():
    threading.Timer(208.0, printit).start()
    draw_art()

timed()

Here is a way you could do it. 这是您可以执行的方法。 In your timed function, you record the start time. 在计时功能中,您记录开始时间。 You then calculate the end time by adding 3 minutes and 30 seconds to the start time. 然后,通过将开始时间增加3分30秒来计算结束时间。

In a loop, you can continue drawing while the current time is less than the end time. 在循环中,您可以在当前时间小于结束时间时继续绘图。

from datetime import datetime, timedelta

def timed():
    start_time = datetime.now()
    end_time = start_time + timedelta(minutes=3, seconds=30)
    while datetime.now() < end_time:
        draw_art()

Here are some resources on the python datetime module, it's the go-to module for any kind of time or date operations! 这是 python datetime模块上的一些资源 ,它是任何时间或日期操作的必备模块!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM