anvas.pack()
self.text = text
self.chars = []
self.target_y = 200 # Final resting height
# Create each character at the top (y=0) with horizontal spacing
for i, char in enumerate(self.text):
char_id = self.canvas.create_text(
100 + (i * 20), 0,
text=char, fill="white",
font=("Arial", 24, "bold")
)
# Store ID and a slight delay for staggered start
self.chars.append({'id': char_id, 'delay': i * 150})
self.animate()
def animate(self):
all_finished = True
for char in self.chars:
# Get current position of the character
coords = self.canvas.coords(char['id'])
# Start falling only if delay has passed and hasn't reached target
if char['delay'] <= 0:
if coords[1] < self.target_y:
self.canvas.move(char['id'], 0, 5) # Move down 5 pixels
all_finished = False
else:
char['delay'] -= 20 # Countdown staggered start
all_finished = False
if not all_finished:
# Repeat function every 20ms for smooth movement
self.root.after(20, self.animate)
if __name__ == "__main__":
root = tk.Tk()
root.title("Falling Animation")
app = FallingTextAnimation(root)
root.mainloop()
Tidak ada komentar:
Posting Komentar