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()

Python Animation Box Moving Forward with Button Click Start Moving

 




import tkinter as tk

import math


def start_animation():

    """Initializes and starts the animation loop."""

    global running

    if not running:

        running = True

        animate()


def animate():

    """Handles the movement and rotation of objects."""

    global rect_pos, wheel1_angle, wheel2_angle, running

    

    if running:

        # Move rectangle forward (right)

        rect_pos += 5

        if rect_pos > 800: rect_pos = -150 # Reset if off-screen

        

        # Redraw components

        canvas.delete("all")

        

        # Draw Brown Rectangle

        canvas.create_rectangle(rect_pos, 100, rect_pos+150, 200, fill="brown", tags="car")

        

        # Draw Wheels (10 spokes)

        for center_x in [rect_pos + 30, rect_pos + 120]:

            center_y = 200

            radius = 25

            canvas.create_oval(center_x-radius, center_y-radius, center_x+radius, center_y+radius, fill="black")

            

            # Draw Spokes

            for i in range(10):

                angle = wheel1_angle + (i * 2 * math.pi / 10)

                end_x = center_x + radius * math.cos(angle)

                end_y = center_y + radius * math.sin(angle)

                canvas.create_line(center_x, center_y, end_x, end_y, fill="white", width=2)

        

        # Rotate Wheels

        wheel1_angle += 0.1

        

        # Loop animation

        root.after(20, animate)


# Setup Window

root = tk.Tk()

root.title("Moving Car Animation")

canvas = tk.Canvas(root, width=800, height=300, bg="white")

canvas.pack()


# Variables

rect_pos = 0

wheel1_angle = 0

running = False


# Button

btn = tk.Button(root, text="Start Moving", command=start_animation)

btn.pack()


root.mainloop()

Python Animation Box with Text inside, Button Click to Start and Moving forward

 


import tkinter as tk
import math

class AnimatedVehicle:
    def __init__(self, root):
        self.root = root
        self.canvas = tk.Canvas(root, width=600, height=300, bg='white')
        self.canvas.pack()

        # Vehicle parameters
        self.x, self.y = 50, 150
        self.wheel_radius = 20
        self.spokes = 10
        self.angle = 0
        self.speed = 5
        self.running = False

        # Draw Vehicle Structure
        self.body = self.canvas.create_rectangle(0, 0, 0, 0, fill='brown')
        self.text = self.canvas.create_text(0, 0, text="Hello", fill='white', font=('Arial', 12))
        self.wheel1 = self.canvas.create_oval(0, 0, 0, 0, outline='black', width=2)
        self.wheel2 = self.canvas.create_oval(0, 0, 0, 0, outline='black', width=2)
        self.spoke_lines = [self.canvas.create_line(0,0,0,0) for _ in range(self.spokes * 2)]
        
        # Initial position
        self.update_vehicle(self.x, self.y)

        # Button
        self.button = tk.Button(root, text="Start", command=self.start_animation)
        self.button.pack()

    def update_vehicle(self, x, y):
        # Update Body
        self.canvas.coords(self.body, x, y - 30, x + 100, y + 10)
        self.canvas.coords(self.text, x + 50, y - 10)
        
        # Update Wheels
        self.canvas.coords(self.wheel1, x + 10, y, x + 10 + 2*self.wheel_radius, y + 2*self.wheel_radius)
        self.canvas.coords(self.wheel2, x + 70, y, x + 70 + 2*self.wheel_radius, y + 2*self.wheel_radius)
        
        # Update Spokes
        for i in range(self.spokes):
            angle = self.angle + i * (360 / self.spokes)
            rad = math.radians(angle)
            
            # Wheel 1 spokes
            self.canvas.coords(self.spoke_lines[i*2], 
                               x + 10 + self.wheel_radius, y + self.wheel_radius,
                               x + 10 + self.wheel_radius + self.wheel_radius * math.cos(rad),
                               y + self.wheel_radius + self.wheel_radius * math.sin(rad))
            # Wheel 2 spokes
            self.canvas.coords(self.spoke_lines[i*2+1], 
                               x + 70 + self.wheel_radius, y + self.wheel_radius,
                               x + 70 + self.wheel_radius + self.wheel_radius * math.cos(rad),
                               y + self.wheel_radius + self.wheel_radius * math.sin(rad))

    def start_animation(self):
        if not self.running:
            self.running = True
            self.button.config(state=tk.DISABLED)
            self.animate()

    def animate(self):
        if self.x > 600:
            self.x = -100
        
        self.x += self.speed
        self.angle += 10 # Rotation speed
        self.update_vehicle(self.x, self.y)
        
        self.root.after(50, self.animate)

root = tk.Tk()
root.title("Moving Vehicle")
app = AnimatedVehicle(root)
root.mainloop()


Python QT Animation

 import sys

import math
from PyQt6.QtWidgets import QApplication, QGraphicsView, QGraphicsScene, QGraphicsItem, QGraphicsRectItem, QGraphicsEllipseItem, QGraphicsTextItem, QGraphicsItemGroup
from PyQt6.QtCore import Qt, QTimer, QRectF, QPropertyAnimation, QPointF, QEasingCurve, QObject, pyqtProperty
from PyQt6.QtGui import QPainter, QPen, QColor, QBrush, QFont

class Wheel(QGraphicsItemGroup):
    def __init__(self, x, y, radius):
        super().__init__()
        self.radius = radius
        # Outer Circle
        circle = QGraphicsEllipseItem(-radius, -radius, 2*radius, 2*radius)
        circle.setBrush(QBrush(QColor("black")))
        circle.setPen(QPen(QColor("gray"), 2))
        self.addToGroup(circle)
        
        # Spokes (10)
        for i in range(10):
            angle = i * (360 / 10)
            spoke = QGraphicsRectItem(-1, -radius, 2, 2*radius)
            spoke.setBrush(QBrush(QColor("gray")))
            spoke.setRotation(angle)
            self.addToGroup(spoke)
        
        self.setPos(x, y)

    def rotate(self, angle):
        for item in self.childItems():
            item.setRotation(item.rotation() + angle)

class Car(QGraphicsItemGroup):
    def __init__(self):
        super().__init__()
        
        # Rectangle
        self.rect = QGraphicsRectItem(0, 0, 200, 80)
        self.rect.setBrush(QBrush(QColor("brown")))
        self.rect.setPen(QPen(QColor("darkred"), 3))
        self.addToGroup(self.rect)
        
        # Text
        self.text = QGraphicsTextItem("I LOVE PYTHON")
        self.text.setFont(QFont("Arial", 12, QFont.Weight.Bold))
        self.text.setDefaultTextColor(QColor("white"))
        self.text.setPos(20, 25)
        self.addToGroup(self.text)
        
        # Wheels
        self.wheel1 = Wheel(40, 80, 20)
        self.wheel2 = Wheel(160, 80, 20)
        self.addToGroup(self.wheel1)
        self.addToGroup(self.wheel2)
        
        # Animation
        self.timer = QTimer()
        self.timer.timeout.connect(self.animate_wheels)
        self.timer.start(50)

    def animate_wheels(self):
        self.wheel1.rotate(10)
        self.wheel2.rotate(10)

class AnimationView(QGraphicsView):
    def __init__(self):
        super().__init__()
        self.scene = QGraphicsScene(0, 0, 800, 300)
        self.setScene(self.scene)
        self.setRenderHint(QPainter.RenderHint.Antialiasing)
        
        self.car = Car()
        self.scene.addItem(self.car)
        
        # Move forward
        self.anim = QPropertyAnimation(self.car, b"pos")
        self.anim.setDuration(5000)
        self.anim.setStartValue(QPointF(-200, 100))
        self.anim.setEndValue(QPointF(800, 100))
        self.anim.setLoopCount(-1)
        self.anim.start()

if __name__ == "__main__":
    app = QApplication(sys.argv)
    view = AnimationView()
    view.show()
    sys.exit(app.exec())

Jumat, 10 April 2026

Python Animation Cart with Text Moving Forward

import tkinter as tk
import math

# --- Constants ---
WIDTH, HEIGHT = 800, 300
WHEEL_RADIUS = 30
WHEEL_DIST = 100
CAR_Y = 200
SPOKES = 10

# --- Setup ---
root = tk.Tk()
root.title("Moving Vehicle")
canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="white")
canvas.pack()

# --- Vehicle Components ---
# Wheels (Circles)
wheel1 = canvas.create_oval(0, 0, 0, 0, outline="brown", width=3)
wheel2 = canvas.create_oval(0, 0, 0, 0, outline="brown", width=3)

# Spokes
spoke_lines = []
for _ in range(SPOKES * 2):
    spoke_lines.append(canvas.create_line(0, 0, 0, 0, fill="brown"))

# Body (Rectangle)
body = canvas.create_rectangle(0, 0, 0, 0, fill="brown", outline="brown")

# Text
text_obj = canvas.create_text(0, 0, text="SMA INFORMATIKA CIAMIS", 

fill="white", font=("Arial", 10, "bold"))

# --- Animation ---
x_pos = 50
angle = 0

def draw_vehicle():
    global angle
    
    # Update positions
    x1 = x_pos
    x2 = x_pos + WHEEL_DIST
    
    # Move wheels
    canvas.coords(wheel1, x1-WHEEL_RADIUS, CAR_Y-WHEEL_RADIUS, 

x1+WHEEL_RADIUS, CAR_Y+WHEEL_RADIUS)
    canvas.coords(wheel2, x2-WHEEL_RADIUS, CAR_Y-WHEEL_RADIUS, 

x2+WHEEL_RADIUS, CAR_Y+WHEEL_RADIUS)
    
    # Move Body
    canvas.coords(body, x1-WHEEL_RADIUS-10, CAR_Y-WHEEL_RADIUS-30, 

x2+WHEEL_RADIUS+10, CAR_Y-10)
    
    # Move Text
    canvas.coords(text_obj, (x1+x2)/2, CAR_Y-45)
    
    # Rotate Spokes
    for i in range(SPOKES):
        a = angle + (i * 2 * math.pi / SPOKES)
        # Spoke 1
        x_s1 = x1 + WHEEL_RADIUS * math.cos(a)
        y_s1 = CAR_Y + WHEEL_RADIUS * math.sin(a)
        canvas.coords(spoke_lines[i*2], x1, CAR_Y, x_s1, y_s1)
        
        # Spoke 2
        x_s2 = x2 + WHEEL_RADIUS * math.cos(a)
        y_s2 = CAR_Y + WHEEL_RADIUS * math.sin(a)
        canvas.coords(spoke_lines[i*2+1], x2, CAR_Y, x_s2, y_s2)

    angle += 0.1 # Rotation speed

def move_vehicle():
    global x_pos
    x_pos += 2
    if x_pos > WIDTH + 100:
        x_pos = -100
        
    draw_vehicle()
    canvas.after(20, move_vehicle)

# --- Start ---
move_vehicle()
root.mainloop()


 

Python Animation Cart Animation Moving Forward Carrying 3 stacked Box

 



import tkinter as tk

import math


# Setup canvas

root = tk.Tk()

root.title("Moving Cart Animation")

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

canvas.pack()


# Cart parameters

cart_x, cart_y = 50, 250

wheel_radius = 20

spokes = 10

angle = 0


def draw_wheel(x, y, r, angle, num_spokes):

    # Wheel rim

    wheel = canvas.create_oval(x-r, y-r, x+r, y+r, outline="black", width=2)

    spoke_lines = []

    # Spokes

    for i in range(num_spokes):

        spoke_angle = angle + (i * 2 * math.pi / num_spokes)

        x_end = x + r * math.cos(spoke_angle)

        y_end = y + r * math.sin(spoke_angle)

        spoke_lines.append(canvas.create_line(x, y, x_end, y_end, fill="black"))

    return wheel, spoke_lines


def animate():

    global cart_x, angle

    canvas.delete("cart") # Clear previous frame


    # Update positions

    cart_x += 5

    angle += 0.2

    if cart_x > 850: cart_x = -100


    # Draw cart body (Brown Rectangle)

    body = canvas.create_rectangle(cart_x, cart_y-40, cart_x+100, cart_y, 


fill="#8B4513", tags="cart")

    

    # Draw boxes

    box1 = canvas.create_rectangle(cart_x+10, cart_y-60, cart_x+30, cart_y-40, 


fill="red", tags="cart")

    box2 = canvas.create_rectangle(cart_x+40, cart_y-60, cart_x+60, cart_y-40, 


fill="blue", tags="cart")

    box3 = canvas.create_rectangle(cart_x+25, cart_y-80, cart_x+45, cart_y-60, 


fill="green", tags="cart")


    # Draw wheels with spokes

    w1_rim, w1_spokes = draw_wheel(cart_x+25, cart_y, wheel_radius, angle, 


spokes)

    w2_rim, w2_spokes = draw_wheel(cart_x+75, cart_y, wheel_radius, angle, 


spokes)

    

    # Add all parts to tags for easy deletion

    for item in [w1_rim, w2_rim] + w1_spokes + w2_spokes:

        canvas.addtag_withtag("cart", item)


    canvas.after(30, animate)


animate()

root.mainloop()

Python Animation Moving Cart Animation


 


import tkinter as tk

import math


# Configuration

WIDTH, HEIGHT = 800, 300

WHEEL_RADIUS = 30

SPOKES = 10

BROWN = "#8B4513"

COLORS = ["red", "blue", "green"]


root = tk.Tk()

root.title("Moving Cart Animation")

canvas = tk.Canvas(root, width=WIDTH, height=HEIGHT, bg="white")

canvas.pack()


# Initial Positions

cart_x = 50

cart_y = 150

angle = 0


def draw_wheel(x, y, r, angle, spokes):

    """Draws a wheel with rotating spokes."""

    wheel = canvas.create_oval(x-r, y-r, x+r, y+r, outline="black", width=2)

    spoke_lines = []

    for i in range(spokes):

        # Calculate spoke angle

        s_angle = angle + (i * (360 / spokes))

        rad = math.radians(s_angle)

        x_end = x + r * math.cos(rad)

        y_end = y + r * math.sin(rad)

        line = canvas.create_line(x, y, x_end, y_end, fill="black")

        spoke_lines.append(line)

    return wheel, spoke_lines


def draw_cart(x, y, color):

    """Draws the brown rectangle and 3 colored boxes."""

    rect = canvas.create_rectangle(x-50, y-30, x+50, y, fill=color, outline="black")

    boxes = []

    box_width = 25

    for i in range(3):

        box_x = x - 40 + (i * (box_width + 5))

        box = canvas.create_rectangle(box_x, y-25, box_x+box_width, y-5, fill=COLORS[i])

        boxes.append(box)

    return rect, boxes


def animate():

    """Main animation loop."""

    global cart_x, angle

    canvas.delete("all")

    

    # Update position and rotation

    cart_x += 2

    angle += 5

    if cart_x > WIDTH + 100:

        cart_x = -100


    # Draw elements

    wheel1, spokes1 = draw_wheel(cart_x - 30, cart_y + 30, WHEEL_RADIUS, angle, SPOKES)

    wheel2, spokes2 = draw_wheel(cart_x + 30, cart_y + 30, WHEEL_RADIUS, angle, SPOKES)

    rect, boxes = draw_cart(cart_x, cart_y, BROWN)

    

    root.after(20, animate)


animate()

root.mainloop()