Sabtu, 11 April 2026

Python Animation Create WindMill

 




import tkinter as tk

import math


class WindmillAnimation:

    def __init__(self, root):

        self.root = root

        self.root.title("Windmill Animation")

        

        # Canvas setup

        self.canvas = tk.Canvas(root, width=400, height=400, bg='white')

        self.canvas.pack()

        

        # Draw stationary building

        self.canvas.create_rectangle(180, 200, 220, 350, fill='brown', 


outline='black')

        

        # Center of the windmill

        self.center_x = 200

        self.center_y = 150

        self.radius = 100

        self.angle = 0

        self.running = False

        

        # Start Button

        self.btn = tk.Button(root, text="Start Windmill", 


command=self.start_animation)

        self.btn.pack()


    def draw_spokes(self):

        self.canvas.delete("spokes") # Remove old spokes

        for i in range(6):

            # Calculate angle for each of the 6 spokes

            theta = math.radians(self.angle + (i * 60))

            x = self.center_x + self.radius * math.cos(theta)

            y = self.center_y + self.radius * math.sin(theta)

            self.canvas.create_line(self.center_x, self.center_y, x, y, 

                                    tags="spokes", fill='black', width=3)

            

        # Draw center hub

        self.canvas.create_oval(self.center_x-5, self.center_y-5, 

                                self.center_x+5, self.center_y+5, 

                                fill='black', tags="spokes")


    def animate(self):

        if self.running:

            self.angle += 5 # Rotation speed

            self.draw_spokes()

            self.root.after(50, self.animate) # 50ms delay [4, 7]


    def start_animation(self):

        if not self.running:

            self.running = True

            self.animate()


if __name__ == "__main__":

    root = tk.Tk()

    app = WindmillAnimation(root)

    root.mainloop()

Tidak ada komentar: