Kamis, 23 April 2026

Python Animation Two Wheels with rectangle and text and controlled by keyboard arrow right and left from and launch missile Bing.com Copilot

  Python Animation Two Wheels with rectangle and text and controlled by keyboard arrow right and left from and launch missile Bing.com Copilot

Prompt :

python tkinter create two circles wheels animation with 4 stokes and create a brown rectangle attach with them with text ="hello" if keyboard arrow press right the wheel rotate and move to right and if keyboard arrow press left the wheel rotate to left and move left and if space key pressed can launch missiles to the top





import tkinter as tk

import math


# --- CONFIG ---

WHEEL_RADIUS = 30

SPOKES = 4

BODY_WIDTH = 150

BODY_HEIGHT = 50

MOVE_SPEED = 5

ROTATE_SPEED = 10  # degrees per move

MISSILE_SPEED = 10


class VehicleAnimation:

    def __init__(self, root):

        self.root = root

        self.root.title("Vehicle Animation with Wheels and Missiles")


        self.canvas = tk.Canvas(root, width=800, height=400, bg="skyblue")

        self.canvas.pack()


        # Initial position

        self.x = 200

        self.y = 300

        self.angle = 0  # wheel rotation angle


        # Store missiles

        self.missiles = []


        # Draw vehicle

        self.draw_vehicle()


        # Bind keys

        self.root.bind("<Right>", self.move_right)

        self.root.bind("<Left>", self.move_left)

        self.root.bind("<space>", self.fire_missile)


        # Animation loop

        self.update_animation()


    def draw_vehicle(self):

        self.canvas.delete("all")


        # Draw wheels

        self.wheel1_center = (self.x, self.y)

        self.wheel2_center = (self.x + BODY_WIDTH - 2*WHEEL_RADIUS, self.y)


        self.draw_wheel(*self.wheel1_center, self.angle)

        self.draw_wheel(*self.wheel2_center, self.angle)


        # Draw body

        body_x1 = self.x - WHEEL_RADIUS

        body_y1 = self.y - WHEEL_RADIUS - BODY_HEIGHT

        body_x2 = body_x1 + BODY_WIDTH

        body_y2 = body_y1 + BODY_HEIGHT

        self.canvas.create_rectangle(body_x1, body_y1, body_x2, body_y2, 


fill="brown")


        # Draw text

        self.canvas.create_text((body_x1 + body_x2) / 2, (body_y1 + body_y2) / 2,

                                text="hello", fill="white", font=("Arial", 16, "bold"))


        # Draw missiles

        for mx, my in self.missiles:

            self.canvas.create_rectangle(mx - 2, my - 10, mx + 2, my, fill="red")


    def draw_wheel(self, cx, cy, angle):

        # Wheel circle

        self.canvas.create_oval(cx - WHEEL_RADIUS, cy - WHEEL_RADIUS,

                                cx + WHEEL_RADIUS, cy + WHEEL_RADIUS, fill="black")


        # Spokes

        for i in range(SPOKES):

            theta = math.radians(angle + i * (360 / SPOKES))

            x_end = cx + WHEEL_RADIUS * math.cos(theta)

            y_end = cy + WHEEL_RADIUS * math.sin(theta)

            self.canvas.create_line(cx, cy, x_end, y_end, fill="white", width=2)


    def move_right(self, event):

        self.x += MOVE_SPEED

        self.angle -= ROTATE_SPEED  # rotate clockwise


    def move_left(self, event):

        self.x -= MOVE_SPEED

        self.angle += ROTATE_SPEED  # rotate counter-clockwise


    def fire_missile(self, event):

        # Missile starts from top center of the body

        missile_x = self.x + BODY_WIDTH / 2 - WHEEL_RADIUS

        missile_y = self.y - WHEEL_RADIUS - BODY_HEIGHT

        self.missiles.append([missile_x, missile_y])


    def update_animation(self):

        # Move missiles upward

        new_missiles = []

        for mx, my in self.missiles:

            my -= MISSILE_SPEED

            if my > 0:

                new_missiles.append([mx, my])

        self.missiles = new_missiles


        # Redraw everything

        self.draw_vehicle()


        # Schedule next frame

        self.root.after(30, self.update_animation)



if __name__ == "__main__":

    root = tk.Tk()

    app = VehicleAnimation(root)

    root.mainloop()

Python Animation Two Wheels with rectangle and text and controlled by keyboard arrow right and left from Gemini AI

 Python Animation Two Wheels with rectangle and text and controlled by keyboard arrow right and left from Gemini AI

Prompt : 


python tkinter create two circles wheels animation with 4 stokes and create a brown rectangle attach with them with text ="hello" if keyboard arrow press right the wheel rotate and move to right and if keyboard arrow press left the wheel rotate to left and move left




import tkinter as tk
import math

# Setup main window
root = tk.Tk()
root.title("Animated Wheels")
canvas = tk.Canvas(root, width=800, height=300, bg='white')
canvas.pack()

# Initial Positions
car_x = 100
car_y = 150
wheel_radius = 20
spokes = 4
angle_offset = 0

# --- Functions to Draw and Move ---
def draw_car(x, y, angle):
    canvas.delete("car") # Clear previous frame

    # Draw Brown Rectangle
    canvas.create_rectangle(x-50, y-30, x+50, y+10, fill='brown', tags="car")
    canvas.create_text(x, y-10, text="hello", fill="white", font=('Arial', 12), 

tags="car")

    # Draw Wheels and Spokes
    for wheel_x in [x-30, x+30]:
        canvas.create_oval(wheel_x-wheel_radius, y+10-wheel_radius, 
                           wheel_x+wheel_radius, y+10+wheel_radius, 
                           fill='black', outline='black', tags="car")
        
        # Draw Spokes
        for i in range(spokes):
            a = angle + (i * 2 * math.pi / spokes)
            # Spoke end points
            x1 = wheel_x
            y1 = y + 10
            x2 = wheel_x + wheel_radius * math.cos(a)
            y2 = y + 10 + wheel_radius * math.sin(a)
            canvas.create_line(x1, y1, x2, y2, fill='white', width=2, tags="car")

def move(event):
    global car_x, angle_offset
    if event.keysym == 'Right':
        car_x += 10
        angle_offset += 0.2
    elif event.keysym == 'Left':
        car_x -= 10
        angle_offset -= 0.2
    
    draw_car(car_x, car_y, angle_offset)

# Bind keys
root.bind('<Left>', move)
root.bind('<Right>', move)

# Initial Draw
draw_car(car_x, car_y, angle_offset)

root.mainloop()



Rabu, 22 April 2026

Python - Animation Wheels WIth Text 3

 




import tkinter as tk

import math


# --- Configuration ---

WIDTH, HEIGHT = 800, 300

RECT_W, RECT_H = 150, 60

WHEEL_RADIUS = 25

SPOKE_COLOR = "white"

ROTATION_SPEED = 15


class Vehicle:

    def __init__(self, canvas, x, y):

        self.canvas = canvas

        self.x = x

        self.y = y

        self.angle = 0

        self.speed = 0

        

        # Create components

        self.body = self.canvas.create_rectangle(

            x, y, x + RECT_W, y + RECT_H, fill="brown", outline="black", width=2)

        self.text = self.canvas.create_text(

            x + RECT_W/2, y + RECT_H/2, text="Hello", fill="white", font=('Arial', 16, 'bold'))

        

        # Wheels

        self.wheel1 = self.create_wheel(x + 30, y + RECT_H)

        self.wheel2 = self.create_wheel(x + RECT_W - 30, y + RECT_H)

        self.wheels = [self.wheel1, self.wheel2]


    def create_wheel(self, cx, cy):

        # Circle

        circle = self.canvas.create_oval(

            cx - WHEEL_RADIUS, cy - WHEEL_RADIUS,

            cx + WHEEL_RADIUS, cy + WHEEL_RADIUS, fill="black")

        

        # 4 Spokes

        spokes = []

        for i in range(4):

            spokes.append(self.canvas.create_line(cx, cy, cx, cy, fill=SPOKE_COLOR, width=2))

        return {'circle': circle, 'spokes': spokes, 'center': (cx, cy)}


    def move(self, direction):

        # Update position

        self.speed = direction * 10

        self.angle += direction * ROTATION_SPEED

        self.canvas.move(self.body, self.speed, 0)

        self.canvas.move(self.text, self.speed, 0)

        

        for wheel in self.wheels:

            self.canvas.move(wheel['circle'], self.speed, 0)

            

            # Rotate Spokes

            cx, cy = wheel['center'][0] + self.speed, wheel['center'][1] # This is wrong, needs to track total move

        

        # Properly move and rotate everything

        self.update_components()


    def update_components(self):

        # Update centers based on total movement

        self.x += self.speed

        

        # Move wheels and re-calculate spokes

        for wheel in self.wheels:

            # Re-center spokes based on current wheel position

            cx = self.canvas.coords(wheel['circle'])[0] + WHEEL_RADIUS

            cy = self.canvas.coords(wheel['circle'])[1] + WHEEL_RADIUS

            wheel['center'] = (cx, cy)

            

            for i in range(4):

                angle = math.radians(self.angle + (i * 90))

                x2 = cx + WHEEL_RADIUS * math.cos(angle)

                y2 = cy + WHEEL_RADIUS * math.sin(angle)

                self.canvas.coords(wheel['spokes'][i], cx, cy, x2, y2)


# --- Setup ---

root = tk.Tk()

root.title("Moving Vehicle")

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

canvas.pack()


vehicle = Vehicle(canvas, 50, 150)


# --- Controls ---

def left(event): vehicle.move(-1)

def right(event): vehicle.move(1)


root.bind("<Left>", left)

root.bind("<Right>", right)


root.mainloop()



Python - Animation Wheels WIth Text 2

 




import tkinter as tk

import math


# Configuration

WIDTH, HEIGHT = 800, 300

WHEEL_RADIUS = 30

SPOKE_LEN = WHEEL_RADIUS - 2

RECT_WIDTH, RECT_HEIGHT = 150, 60

SPEED = 5

ANGLE_STEP = 0.2


# Initialize Tkinter

root = tk.Tk()

root.title("Animated Wagon")

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

canvas.pack()


# Initial Positions

rect_x = 50

rect_y = HEIGHT - 100

angle = 0


def draw_spokes(x, y, angle):

    """Draws 4 rotating spokes for a wheel."""

    spokes = []

    for i in range(4):

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

        x1 = x + SPOKE_LEN * math.cos(a)

        y1 = y + SPOKE_LEN * math.sin(a)

        x2 = x - SPOKE_LEN * math.cos(a)

        y2 = y - SPOKE_LEN * math.sin(a)

        spokes.append(canvas.create_line(x1, y1, x2, y2, fill="white", width=2))

    return spokes


def animate():

    global rect_x, angle

    

    # Update positions

    rect_x += SPEED

    angle += ANGLE_STEP

    

    # Wrap around screen

    if rect_x > WIDTH:

        rect_x = -RECT_WIDTH

        

    # Clear and Redraw Everything

    canvas.delete("all")

    

    # Draw Brown Rectangle

    canvas.create_rectangle(rect_x, rect_y, rect_x + RECT_WIDTH, rect_y + RECT_HEIGHT, fill="#8B4513", outline="")

    canvas.create_text(rect_x + RECT_WIDTH/2, rect_y + RECT_HEIGHT/2, text="hello", fill="white", font=('Arial', 16))

    

    # Wheel Centers

    wh1_x, wh2_x = rect_x + 30, rect_x + RECT_WIDTH - 30

    wh_y = rect_y + RECT_HEIGHT + 10

    

    # Draw Wheels (circles)

    for cx in [wh1_x, wh2_x]:

        canvas.create_oval(cx-WHEEL_RADIUS, wh_y-WHEEL_RADIUS, cx+WHEEL_RADIUS, wh_y+WHEEL_RADIUS, fill="black", outline="white")

        draw_spokes(cx, wh_y, angle)

        

    root.after(30, animate)


# Start animation

animate()

root.mainloop()

Python - Animation Wheels WIth Text 1

 



import tkinter as tk

import math


# --- Configuration ---

WINDOW_WIDTH = 800

WINDOW_HEIGHT = 400

WHEEL_RADIUS = 30

SPOKES = 4

SPEED = 3  # pixels per frame

ROTATION_SPEED = 5  # degrees per frame


class MovingCar:

    def __init__(self, canvas):

        self.canvas = canvas

        self.angle = 0

        self.x = 50

        self.y = 250


        # Create wheels

        self.left_wheel = self.canvas.create_oval(

            self.x, self.y,

            self.x + WHEEL_RADIUS * 2, self.y + WHEEL_RADIUS * 2,

            outline="black", width=2

        )

        self.right_wheel = self.canvas.create_oval(

            self.x + 120, self.y,

            self.x + 120 + WHEEL_RADIUS * 2, self.y + WHEEL_RADIUS * 2,

            outline="black", width=2

        )


        # Create spokes for each wheel

        self.left_spokes = []

        self.right_spokes = []

        for _ in range(SPOKES):

            self.left_spokes.append(self.canvas.create_line(0, 0, 0, 0, fill="black", width=2))

            self.right_spokes.append(self.canvas.create_line(0, 0, 0, 0, fill="black", width=2))


        # Create rectangle body

        self.body = self.canvas.create_rectangle(

            self.x - 10, self.y - 50,

            self.x + 160, self.y,

            fill="brown"

        )


        # Create text

        self.text = self.canvas.create_text(

            self.x + 75, self.y - 25,

            text="hello", fill="white", font=("Arial", 14, "bold")

        )


    def update_spokes(self, wheel_center_x, wheel_center_y, spokes_list):

        """Update spoke positions based on current rotation angle."""

        for i, spoke in enumerate(spokes_list):

            angle_deg = self.angle + (360 / SPOKES) * i

            rad = math.radians(angle_deg)

            x_end = wheel_center_x + WHEEL_RADIUS * math.cos(rad)

            y_end = wheel_center_y + WHEEL_RADIUS * math.sin(rad)

            self.canvas.coords(spoke, wheel_center_x, wheel_center_y, x_end, y_end)


    def move(self):

        # Move all parts to the right

        self.canvas.move(self.left_wheel, SPEED, 0)

        self.canvas.move(self.right_wheel, SPEED, 0)

        self.canvas.move(self.body, SPEED, 0)

        self.canvas.move(self.text, SPEED, 0)

        for spoke in self.left_spokes + self.right_spokes:

            self.canvas.move(spoke, SPEED, 0)


        # Update rotation

        self.angle = (self.angle + ROTATION_SPEED) % 360


        # Get wheel centers

        lx1, ly1, lx2, ly2 = self.canvas.coords(self.left_wheel)

        rx1, ry1, rx2, ry2 = self.canvas.coords(self.right_wheel)

        left_center = ((lx1 + lx2) / 2, (ly1 + ly2) / 2)

        right_center = ((rx1 + rx2) / 2, (ry1 + ry2) / 2)


        # Update spokes

        self.update_spokes(*left_center, self.left_spokes)

        self.update_spokes(*right_center, self.right_spokes)


        # Loop animation

        if lx1 < WINDOW_WIDTH:

            self.canvas.after(30, self.move)


# --- Main Program ---

root = tk.Tk()

root.title("Rotating Wheels Animation")


canvas = tk.Canvas(root, width=WINDOW_WIDTH, height=WINDOW_HEIGHT, bg="skyblue")

canvas.pack()


car = MovingCar(canvas)

car.move()


root.mainloop()

Python - Animation Wheels WIth Text And Missile

 



import tkinter as tk

import math


# --- Configuration ---

W, H = 800, 400

WHEEL_RADIUS = 30

SPOKES = 4

VEHICLE_SPEED = 3

ROTATION_SPEED = 5  # degrees per frame

MISSILE_SPEED = 8


class VehicleAnimation:

    def __init__(self, root):

        self.root = root

        self.canvas = tk.Canvas(root, width=W, height=H, bg="skyblue")

        self.canvas.pack()


        # Initial position

        self.x = 100

        self.y = 250

        self.angle = 0

        self.missiles = []


        # Draw wheels

        self.left_wheel = self.canvas.create_oval(

            self.x, self.y, self.x + WHEEL_RADIUS*2, self.y + WHEEL_RADIUS*2, fill="black"

        )

        self.right_wheel = self.canvas.create_oval(

            self.x + 120, self.y, self.x + 120 + WHEEL_RADIUS*2, self.y + WHEEL_RADIUS*2, fill="black"

        )


        # Draw spokes

        self.left_spokes = []

        self.right_spokes = []

        for _ in range(SPOKES):

            self.left_spokes.append(self.canvas.create_line(0, 0, 0, 0, fill="white", width=2))

            self.right_spokes.append(self.canvas.create_line(0, 0, 0, 0, fill="white", width=2))


        # Draw vehicle body

        self.body = self.canvas.create_rectangle(

            self.x + 10, self.y - 40, self.x + 170, self.y, fill="saddlebrown"

        )


        # Add text

        self.text = self.canvas.create_text(

            self.x + 90, self.y - 20, text="hello", font=("Arial", 14), fill="white"

        )


        # Draw cannon

        self.cannon = self.canvas.create_rectangle(

            self.x + 150, self.y - 50, self.x + 170, self.y - 40, fill="gray"

        )


        # Bind space key for firing

        self.root.bind("<space>", self.fire_missile)


        # Start animation

        self.animate()


    def draw_spokes(self, wheel_center_x, wheel_center_y, spokes_list):

        """Draw rotating spokes for a wheel."""

        for i in range(SPOKES):

            angle_deg = self.angle + (360 / SPOKES) * i

            angle_rad = math.radians(angle_deg)

            x1 = wheel_center_x + math.cos(angle_rad) * WHEEL_RADIUS

            y1 = wheel_center_y + math.sin(angle_rad) * WHEEL_RADIUS

            x2 = wheel_center_x - math.cos(angle_rad) * WHEEL_RADIUS

            y2 = wheel_center_y - math.sin(angle_rad) * WHEEL_RADIUS

            self.canvas.coords(spokes_list[i], x1, y1, x2, y2)


    def fire_missile(self, event=None):

        """Create a missile from the cannon."""

        missile = self.canvas.create_rectangle(

            self.x + 170, self.y - 45, self.x + 180, self.y - 40, fill="red"

        )

        self.missiles.append(missile)


    def animate(self):

        # Move vehicle

        self.x += VEHICLE_SPEED

        self.angle = (self.angle + ROTATION_SPEED) % 360


        # Update wheel positions

        self.canvas.coords(self.left_wheel,

                           self.x, self.y,

                           self.x + WHEEL_RADIUS*2, self.y + WHEEL_RADIUS*2)

        self.canvas.coords(self.right_wheel,

                           self.x + 120, self.y,

                           self.x + 120 + WHEEL_RADIUS*2, self.y + WHEEL_RADIUS*2)


        # Update spokes

        self.draw_spokes(self.x + WHEEL_RADIUS, self.y + WHEEL_RADIUS, self.left_spokes)

        self.draw_spokes(self.x + 120 + WHEEL_RADIUS, self.y + WHEEL_RADIUS, self.right_spokes)


        # Update body, text, cannon

        self.canvas.coords(self.body,

                           self.x + 10, self.y - 40,

                           self.x + 170, self.y)

        self.canvas.coords(self.text,

                           self.x + 90, self.y - 20)

        self.canvas.coords(self.cannon,

                           self.x + 150, self.y - 50,

                           self.x + 170, self.y - 40)


        # Move missiles

        for missile in list(self.missiles):

            self.canvas.move(missile, MISSILE_SPEED, 0)

            coords = self.canvas.coords(missile)

            if coords[0] > W:  # Remove if off-screen

                self.canvas.delete(missile)

                self.missiles.remove(missile)


        # Loop animation

        self.root.after(30, self.animate)



if __name__ == "__main__":

    root = tk.Tk()

    root.title("Vehicle Animation with Cannon")

    app = VehicleAnimation(root)

    root.mainloop()



Sabtu, 18 April 2026

Python XML 4

 



import tkinter as tk
from tkinter import ttk
import xml.etree.ElementTree as ET

# 1. Sample XML Data (could be loaded from a file)
xml_data = """<?xml version="1.0"?>
<library>
    <book id="1">
        <title>Python Basics</title>
        <author>John Doe</author>
        <year>2020</year>
    </book>
    <book id="2">
        <title>Tkinter Guide</title>
        <author>Jane Smith</author>
        <year>2022</year>
    </book>
</library>
"""

def load_xml_data(tree):
    # Parse XML
    root = ET.fromstring(xml_data)
    
    # Iterate over XML elements and insert into treeview
    for book in root.findall('book'):
        book_id = book.get('id')
        title = book.find('title').text
        author = book.find('author').text
        year = book.find('year').text
        tree.insert("", "end", values=(book_id, title, author, year))

# Create GUI
root = tk.Tk()
root.title("XML to Treeview")

# 2. Setup Treeview
columns = ("id", "title", "author", "year")
tree = ttk.Treeview(root, columns=columns, show="headings")

# Define Headings
tree.heading("id", text="ID")
tree.heading("title", text="Title")
tree.heading("author", text="Author")
tree.heading("year", text="Year")

# Define Columns
tree.column("id", width=50)
tree.column("title", width=200)
tree.column("author", width=150)
tree.column("year", width=50)

tree.pack(pady=20)

# Load data
load_xml_data(tree)

root.mainloop()