I want to create a purple rain in Python.
Here is the code:
from tkinter import *
import random
import time
principal = Tk()
principal.title('Lluvia')
principal.geometry('500x300')
principal.resizable(0, 0)
canvas = Canvas(principal, width = 600, height = 300, bg = 'white')
class gota(object):
def __init__(self):
x = random.randrange(0, 500)
y = random.randrange(-150, -30)
l = random.randrange(10, 30)
self.t = l // 10
self.gota = canvas.create_rectangle(x, y, x + self.t, y + l, outline="", fill = '#8F00FF')
canvas.pack()
principal.after(0, self.animacion)
def animacion(self):
while(True):
time.sleep(0.0001)
canvas.move(self.gota, 0, self.t * 2)
canvas.update()
coordenadas = canvas.coords(self.gota)
if (coordenadas[1] > 310):
canvas.move(self.gota, 0, -350)
canvas.update()
I can create one drop but the problem is that I want to create multiple drops and all of them need to update at the same time, I tried using threading:
threads = []
for i in range(1):
t = threading.Thread(target = gota)
t.daemon = True
threads.append(t)
for t in threads:
t.start()
for t in threads:
t.join()
But it doesn't work because I get the error:
RuntimeError: main thread is not in main loop
It's there a way to do what I'm trying to do in Tkinter? Thanks!