F
F
FlakeSunrise2020-05-30 13:16:25
Python
FlakeSunrise, 2020-05-30 13:16:25

What is the problem with the loop and the time library?

Greetings, I recently took up the task of making a loop in Python that will decrease or increase the circle every second, but I ran into a problem that the loop is very fast, I tried to use time.sleep(), but the code just freezes, I have been trying to find a solution for about a week now.

import tkinter as tk
import time
root = tk.Tk()
canvas = tk.Canvas(root, width=2000, height=2000, borderwidth=0, highlightthickness=0, bg="black")
canvas.grid()

def _create_circle(self, x, y, r, **kwargs):
    return self.create_oval(x-r, y-r, x+r, y+r, **kwargs)
tk.Canvas.create_circle = _create_circle
def _create_circle_arc(self, x, y, r, **kwargs):
    if "start" in kwargs and "end" in kwargs:
        kwargs["extent"] = kwargs["end"] - kwargs["start"]
        del kwargs["end"]
    return self.create_arc(x-r, y-r, x+r, y+r, **kwargs)
tk.Canvas.create_circle_arc = _create_circle_arc
for r in range(200, 100, -5):
  canvas.create_circle(725, 500, r, fill="blue", outline="red", width=4)
root.wm_title("Circles and Arcs")
root.mainloop()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan Yakushenko, 2020-05-30
@FlakeSunrise

Tkinter has some analogue sleep - after , after the specified time has elapsed, it calls the desired function.
But in your case it is useless, because you first create the object, and only then launch the window itself. You can do this:

for r in range(200, 100, -5):
    print(r)
    canvas.create_circle(725, 500, r, fill="blue", outline="red", width=4)

root.wm_title("Circles and Arcs")
print(True)
root.mainloop()

As you can see mainloop, it was not launched, but the function canvas.create_circlewas still executed, and only then it will be displayed in the console Trueand the launch will take place. mainloop
Do you want to draw dynamically? Whereas, as an option, you can start rendering in a separate thread, and even time.sleepshove it there, here's an example on your task:
import tkinter as tk
from threading import Thread
from time import sleep


class CreateCircle(tk.Tk):
    def __init__(self, width=1000, height=1000, borderwidth=0, highlightthickness=0, bg='black'):
        tk.Tk.__init__(self)
        self.canvas = tk.Canvas(
            self, width=width, height=height, borderwidth=borderwidth,
            highlightthickness=highlightthickness, bg=bg
        )
        self.create_circle_thread = Thread(target=self.create_circle)
        self.create_circle_thread.start()
        self.canvas.pack()
        self.run()

    def create_circle(self, x=725, y=500, fill='blue', outline='red', width=4):
        for r in range(200, 100, -5):
            self.__create_circle(x, y, r, fill=fill, outline=outline, width=width)
            sleep(0.2)
    
    def __create_circle(self, x, y, r, **kwargs):
        return self.canvas.create_oval(x-r, y-r, x+r, y+r, **kwargs)

    def run(self):
        self.mainloop()


cc = CreateCircle()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question