ARTICLE AD BOX
This is the error messege i get
Traceback (most recent call last):
File "Pet.py", line 153, in <module>
File "tkinter\_init_.py", line 2528, in destroy
_tkinter.TclError: can't invoke "destroy" command: application has been destroyed
This is My code:
from tkinter import *
from tkinter import messagebox
import random
# =====================================================================
# 📖 EMBEDDED "HOW TO PLAY" INSTRUCTIONS MANUAL
# =====================================================================
INSTRUCTIONS_TEXT = """========================================================================
DESKTOP COMPANION - INSTRUCTIONS MANUAL========================================================================
Welcome to your custom Desktop Companion! Here is exactly how to interact
with your new digital friend:
1. SELECTING YOUR COMPANION
* Choose from the 4 pre-made vector animals (Cat, Fox, Frog, Slime).
* Or, click 'Open Pixel Art Studio' to paint your own custom creation!
2. CONTROLLING YOUR PET
* LEFT-CLICK & HOLD anywhere on the animal to pick them up.
* DRAG your mouse to slide or fly them anywhere around your desktop.
* RELEASE the mouse button to drop them back onto your screen.
3. PIXEL ART STUDIO CONTROLS
* LEFT-CLICK & DRAG across the grid cells to paint with your active color.
* RIGHT-CLICK & DRAG across the grid cells to erase pixels back to blank space.
* Select different colors from the top palette bar.
* Click 'Spawn! 🚀' when you are finished to release them onto your display.
4. TURNING THE PET OFF
* When it is time to work or clear your desktop, simply tap the LEFT ALT
or RIGHT ALT key on your keyboard. The process will close instantly.
Have fun letting your new companions explore your wallpaper hills!
========================================================================"""
def show_embedded_instructions():
ins_win = Toplevel() ins_win.title("How to Play / Instructions") ins_win.attributes('-topmost', True) ins_win.geometry("500x380") \# Scrollbar setup for easy reading scrollbar = Scrollbar(ins_win) scrollbar.pack(side=RIGHT, fill=Y) text_widget = Text(ins_win, wrap=WORD, yscrollcommand=scrollbar.set, font=("Courier", 10)) text_widget.insert(END, INSTRUCTIONS_TEXT) text_widget.config(state=DISABLED) # Lock text from being edited by user text_widget.pack(side=LEFT, fill=BOTH, expand=True) scrollbar.config(command=text_widget.yview)# =====================================================================
# 🎛️ STEP 1: COMPANION SELECTOR MENU WINDOW
# =====================================================================
root_init = Tk()
root_init.title("Pet Selector")
root_init.attributes('-topmost', True)
root_init.geometry("280x210")
Label(root_init, text="Choose Your Desktop Companion:", font=("Arial", 10, "bold")).pack(pady=5)
active_animal_index = 0
custom_pixel_matrix = None
# Frame to cleanly hold preset buttons
btn_frame = Frame(root_init)
btn_frame.pack(pady=5)
def assign_and_close(index_num):
global active_animal_index active_animal_index = index_num if index_num != 4: root_init.quit() else: open_pixel_art_maker()# Preset Selection Buttons
Button(btn_frame, text="🐈 Cat", width=12, command=lambda: assign_and_close(0)).grid(row=0, column=0, padx=5, pady=2)
Button(btn_frame, text="🦊 Fox", width=12, command=lambda: assign_and_close(1)).grid(row=0, column=1, padx=5, pady=2)
Button(btn_frame, text="🐸 Frog", width=12, command=lambda: assign_and_close(2)).grid(row=1, column=0, padx=5, pady=2)
Button(btn_frame, text="💧 Slime", width=12, command=lambda: assign_and_close(3)).grid(row=1, column=1, padx=5, pady=2)
Button(root_init, text="🎨 Open Pixel Art Studio", bg="#2196F3", fg="white", width=26, command=lambda: assign_and_close(4)).pack(pady=2)
# Baked-in Instructions Guide Button
Button(root_init, text="❓ How to Play / Help", bg="#4CAF50", fg="white", width=26, command=show_embedded_instructions).pack(pady=2)
def open_pixel_art_maker():
global custom_pixel_matrix designer = Toplevel() designer.title("Pixel Pet Studio") designer.attributes('-topmost', True) GRID_ROWS, GRID_COLS, PIXEL_SIZE = 12, 12, 20 current_color = "orange" color_buttons = {} grid_cells = \[\[None for \_ in range(GRID_COLS)\] for \_ in range(GRID_ROWS)\] controls = Frame(designer) controls.pack(side=TOP, fill=X, padx=5, pady=5) grid_frame = Frame(designer) grid_frame.pack(side=BOTTOM, padx=5, pady=5) def set_active_color(color_name): nonlocal current_color current_color = color_name for col, btn in color_buttons.items(): btn.config(relief=RAISED, bd=2) color_buttons\[color_name\].config(relief=SUNKEN, bd=4) palette = \["orange", "red", "yellow", "green", "blue", "purple", "pink", "black", "white", "brown"\] for col in palette: btn = Button(controls, bg=col, width=2, command=lambda c=col: set_active_color(c)) btn.pack(side=LEFT, padx=2) color_buttons\[col\] = btn set_active_color("orange") def draw_pixel(event): col, row = event.x // PIXEL_SIZE, event.y // PIXEL_SIZE if 0 \<= col \< GRID_COLS and 0 \<= row \< GRID_ROWS: designer_canvas.itemconfig(grid_cells\[row\]\[col\], fill=current_color) def erase_pixel(event): col, row = event.x // PIXEL_SIZE, event.y // PIXEL_SIZE if 0 \<= col \< GRID_COLS and 0 \<= row \< GRID_ROWS: designer_canvas.itemconfig(grid_cells\[row\]\[col\], fill="") designer_canvas = Canvas(grid_frame, width=GRID_COLS\*PIXEL_SIZE, height=GRID_ROWS\*PIXEL_SIZE, bg="#dcdcdc") designer_canvas.pack() for r in range(GRID_ROWS): for c in range(GRID_COLS): grid_cells\[r\]\[c\] = designer_canvas.create_rectangle(c\*PIXEL_SIZE, r\*PIXEL_SIZE, (c+1)\*PIXEL_SIZE, (r+1)\*PIXEL_SIZE, fill="", outline="#b0b0b0") designer_canvas.bind("\<B1-Motion\>", draw_pixel); designer_canvas.bind("\<Button-1\>", draw_pixel) designer_canvas.bind("\<B3-Motion\>", erase_pixel); designer_canvas.bind("\<Button-3\>", erase_pixel) def finalize_and_save(): global custom_pixel_matrix matrix_data = \[\] for r in range(GRID_ROWS): row_data = \[\] for c in range(GRID_COLS): cell_color = designer_canvas.itemcget(grid_cells\[r\]\[c\], "fill") row_data.append(cell_color if cell_color != "" else None) matrix_data.append(row_data) custom_pixel_matrix = matrix_data root_init.quit() Button(controls, text="Spawn! 🚀", bg="#4CAF50", fg="white", command=finalize_and_save).pack(side=RIGHT, padx=5) designer.protocol("WM_DELETE_WINDOW", root_init.quit)root_init.mainloop()
root_init.destroy()
# =====================================================================
# 🏃♂️ STEP 2: FULL-SCREEN 2D MOVEMENT LOOP ENGINE
# =====================================================================
window = Tk()
window.overrideredirect(True)
window.attributes('-topmost', True)
transparent_color = '#abcdef'
window.config(bg=transparent_color)
window.wm_attributes('-transparentcolor', transparent_color)
canvas_width, canvas_height = 90, 60
canvas = Canvas(window, width=canvas_width, height=canvas_height, bg=transparent_color, bd=0, highlightthickness=0)
canvas.pack()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
x = random.randint(100, screen_width - 100)
y = random.randint(100, screen_height - 150)
mouse_offset_x = 0; mouse_offset_y = 0
is_dragged = False
current_direction = "right"
dx, dy = 0, 0
leg_toggle = True
def render_pet(pose):
global leg_toggle canvas.delete("all") canvas.create_rectangle(0, 0, canvas_width, canvas_height, fill=transparent_color, outline='') if active_animal_index == 4 and custom_pixel_matrix: rows, cols = len(custom_pixel_matrix), len(custom_pixel_matrix) px_scale, start_x, start_y = 4, 20, 6 for r in range(rows): for c in range(cols): target_col = c if current_direction == "right" else (cols - 1 - c) pixel_color = custom_pixel_matrix\[r\]\[target_col\] if pixel_color: canvas.create_rectangle(start_x + c \* px_scale, start_y + r \* px_scale, start_x + (c + 1) \* px_scale, start_y + (r + 1) \* px_scale, fill=pixel_color, outline='') else: if active_animal_index == 1: # FOX if current_direction == "right": canvas.create_rectangle(15, 20, 50, 38, fill='#e65c00', outline='') canvas.create_rectangle(45, 12, 60, 28, fill='#e65c00', outline='') canvas.create_polygon(50, 12, 53, 2, 57, 12, fill='#b34700', outline='') canvas.create_rectangle(54, 16, 57, 19, fill='black', outline='') canvas.create_rectangle(5, 12, 15, 24, fill='#e65c00', outline='') canvas.create_rectangle(2, 12, 5, 20, fill='white', outline='') else: canvas.create_rectangle(40, 20, 75, 38, fill='#e65c00', outline='') canvas.create_rectangle(30, 12, 45, 28, fill='#e65c00', outline='') canvas.create_polygon(33, 12, 37, 2, 40, 12, fill='#b34700', outline='') canvas.create_rectangle(33, 16, 36, 19, fill='black', outline='') canvas.create_rectangle(75, 12, 85, 24, fill='#e65c00', outline='') canvas.create_rectangle(85, 12, 88, 20, fill='white', outline='') canvas.create_rectangle(22, 38, 25, 46, fill='black', outline='') canvas.create_rectangle(45, 38, 48, 46, fill='black', outline='') elif active_animal_index == 2: # FROG canvas.create_rectangle(20, 20, 60, 42, fill='#2ecc71', outline='') canvas.create_rectangle(25, 12, 35, 20, fill='#2ecc71', outline='') canvas.create_rectangle(45, 12, 55, 20, fill='#2ecc71', outline='') canvas.create_rectangle(28, 15, 32, 18, fill='black', outline='') canvas.create_rectangle(48, 15, 52, 18, fill='black', outline='') if leg_toggle: canvas.create_rectangle(10, 32, 20, 42, fill='#27ae60', outline='') canvas.create_rectangle(60, 32, 70, 42, fill='#27ae60', outline='') else: canvas.create_rectangle(15, 36, 25, 44, fill='#27ae60', outline='') canvas.create_rectangle(55, 36, 65, 44, fill='#27ae60', outline='') leg_toggle = not leg_toggle elif active_animal_index == 3: # SLIME if leg_toggle: canvas.create_rectangle(25, 15, 65, 44, fill='#00d2ff', outline='') canvas.create_rectangle(35, 25, 40, 30, fill='white', outline='') canvas.create_rectangle(50, 25, 55, 30, fill='white', outline='') else: canvas.create_rectangle(20, 24, 70, 44, fill='#00d2ff', outline='') canvas.create_rectangle(32, 30, 37, 35, fill='white', outline='') canvas.create_rectangle(53, 30, 58, 35, fill='white', outline='') leg_toggle = not leg_toggle else: # CAT if current_direction == "right": canvas.create_rectangle(15, 20, 50, 38, fill='#ff943d', outline='') canvas.create_rectangle(45, 10, 62, 28, fill='#ff943d', outline='') canvas.create_polygon(47, 10, 52, 2, 54, 10, fill='#d46a11', outline='') canvas.create_polygon(55, 10, 60, 2, 62, 10, fill='#d46a11', outline='') canvas.create_rectangle(55, 14, 59, 18, fill='white', outline='') canvas.create_rectangle(57, 15, 59, 18, fill='black', outline='') canvas.create_rectangle(8, 14, 15, 24, fill='#ff943d', outline='') else: canvas.create_rectangle(40, 20, 75, 38, fill='#ff943d', outline='') canvas.create_rectangle(28, 10, 45, 28, fill='#ff943d', outline='') canvas.create_polygon(30, 10, 35, 2, 37, 10, fill='#d46a11', outline='') canvas.create_polygon(38, 10, 43, 2, 45, 10, fill='#d46a11', outline='') canvas.create_rectangle(31, 14, 35, 18, fill='white', outline='') canvas.create_rectangle(31, 15, 33, 18, fill='black', outline='') canvas.create_rectangle(75, 14, 82, 24, fill='#ff943d', outline='') if leg_toggle: canvas.create_rectangle(25, 38, 29, 46, fill='#d46a11', outline='') canvas.create_rectangle(61, 38, 65, 46, fill='#d46a11', outline='') else: canvas.create_rectangle(32, 38, 36, 46, fill='#d46a11', outline='') canvas.create_rectangle(54, 38, 58, 46, fill='#d46a11', outline='') leg_toggle = not leg_toggledef start_drag(event):
global mouse_offset_x, mouse_offset_y, is_dragged, dx, dy is_dragged = True; dx, dy = 0, 0 mouse_offset_x, mouse_offset_y = event.x, event.ydef drag_motion(event):
global x, y if is_dragged: x = window.winfo_x() - mouse_offset_x + event.x y = window.winfo_y() - mouse_offset_y + event.y window.geometry(f'{canvas_width}x{canvas_height}+{x}+{y}')def stop_drag(event):
global is_dragged is_dragged = Falsedef update_pet():
global x, y, dx, dy, current_direction if not is_dragged: if random.randint(1, 15) == 1 or (dx == 0 and dy == 0): state = random.choice(\['move', 'move', 'float_up', 'float_down', 'idle'\]) if state == 'move': dx = random.choice(\[-12, -8, 8, 12\]); dy = random.choice(\[-8, -4, 4, 8\]) elif state == 'float_up': dx = random.choice(\[-6, 6\]); dy = -10 elif state == 'float_down': dx = random.choice(\[-6, 6\]); dy = 10 elif state == 'idle': dx, dy = 0, 0 x += dx; y += dy if dx \< 0: current_direction = "left" elif dx \> 0: current_direction = "right" if x \< 0: x = 0; dx = -dx; current_direction = "right" elif x \> screen_width - canvas_width: x = screen_width - canvas_width; dx = -dx; current_direction = "left" if y \< 0: y = 0; dy = -dy elif y \> screen_height - canvas_height - 40: y = screen_height - canvas_height - 40; dy = -dy render_pet("walk") window.geometry(f'{canvas_width}x{canvas_height}+{x}+{y}') window.after(250, update_pet)window.bind('<Alt_L>', lambda e: window.destroy())
window.bind('<Alt_R>', lambda e: window.destroy())
canvas.bind('<Button-1>', start_drag)
canvas.bind('<B1-Motion>', drag_motion)
canvas.bind('<ButtonRelease-1>', stop_drag)
update_pet()
window.mainloop()
I worked really hard on this game i made this for my mom since her Birthday is coming up Please someone help
