The idea is to create rain. In the class "Drop" there are 2 methods: show (), which creates a line, and update (), which updates the coordinates, everything can be seen in the code:

def show(self): c.create_line(self.x, self.y, self.x, self.y+const, fill = "white") def update(self): self.y += self.yspeed drops = [Raindrop() for i in range(101)] def main(): for i in range(len(drops)): drops[i].show() drops[i].update() root.after(30,main) main() root.mainloop() 

A line will be drawn, then the coordinates will be updated and cyclically. It is logical that a new line will be drawn on the new coordinates, and the old line with the old coordinates will remain. This is actually the question: how to write the code to avoid it, i.e. When a new line is drawn on the new coordinates, the old one is erased?

    1 answer 1

    You can not erase / draw again, and move the old line. Code example:

     import tkinter as tk from random import randint class Raindrop: def __init__(self, canvas, x, y, yspeed, length, color='blue'): self.x = x self.y = y self.yspeed = yspeed self.length = length self.canvas = canvas self.line = canvas.create_line(self.x, self.y, self.x, self.y+length, fill=color) def move(self): self.y += self.yspeed self.canvas.move(self.line, 0, self.yspeed) # При падении за нижний край холста передвигаем каплю выше верхнего края холста # Здесь размер холста жестко задан, но можно определять его размер и программно if self.y > 500: self.canvas.move(self.line, 0, -(500+self.length)) self.y -= 500 + self.length def redraw(): for drop in drops: drop.move() root.after(10, redraw) root = tk.Tk() canvas = tk.Canvas(root, width=500, height=500) canvas.pack() drops = [Raindrop(canvas, x=randint(0, 500), y=randint(0, 500), yspeed=randint(1, 3), length=randint(5, 20)) for i in range(101)] redraw() root.mainloop() 

    Animation