Skip to content

hero-programmers/spider-attack

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

8 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

# 🕷️ Spider Attack (Tkinter GUI Project)

**Spider Attack** is a fun and interactive Python project that displays randomly spawning spider images across your entire screen.  
It’s built using **Tkinter** for the GUI and **Pillow (PIL)** for image handling.

---

## 🚀 Features

- Fullscreen immersive window.  
- Always stays on top of all other windows.  
- Transparent black background (on Windows).  
- Spiders appear randomly every 0.5 seconds.  
- Designed as a visual prank or creative GUI demo.  

---

## 🧩 Code Overview

```python
import tkinter as tk
from PIL import Image, ImageTk
import random

root = tk.Tk()
root.attributes('-fullscreen', True)
root.attributes('-topmost', True)
root.attributes('-transparentcolor', 'black')

w = root.winfo_screenwidth()
h = root.winfo_screenheight()

canvas = tk.Canvas(root, width=w, height=h, bg='black', highlightthickness=0)
canvas.pack()

img = Image.open("spider.png")
spider_img = ImageTk.PhotoImage(img)

def add_spider():
    x = random.randint(0, w)
    y = random.randint(0, h)
    canvas.create_image(x, y, image=spider_img)
    root.after(500, add_spider)

root.protocol('WM_DELETE_WINDOW', lambda: None)
add_spider()
root.mainloop()

⚙️ How It Works

  1. The program opens a Tkinter fullscreen window.
  2. The window stays always on top of other apps.
  3. The -transparentcolor attribute makes all black areas invisible (Windows only).
  4. A spider image is loaded from spider.png.
  5. Every 500 ms, a new spider is drawn at a random location on the screen.
  6. The window can’t be closed using the normal “X” button, creating a continuous effect.

🧠 Requirements

Install the required library:

pip install pillow

▶️ Running the Project

  1. Place a transparent-background image named spider.png in the same folder as your script.

  2. Run the program:

    python main.py
  3. Enjoy watching spiders crawl randomly on your screen.

To exit:

  • Windows: Press Alt + F4
  • macOS: Press Cmd + Q
  • Or stop the program in the terminal with Ctrl + C.

🪟 Windows Users

This code works perfectly on Windows, where -transparentcolor 'black' removes all black pixels from the window — giving a realistic transparent effect where spiders appear to crawl over your desktop and other windows.


🍏 macOS Users

❌ Error

If you run this program on macOS, you’ll likely see the following error:

_tkinter.TclError: bad attribute "-transparentcolor"

🔍 Cause

The attribute -transparentcolor works only on Windows, not on macOS or Linux. macOS uses a different underlying window system (Cocoa) that Tkinter doesn’t expose color-based transparency for.


✅ Fix (for macOS)

Use the -transparent and -alpha attributes instead:

import tkinter as tk
from PIL import Image, ImageTk
import random

root = tk.Tk()
root.attributes('-fullscreen', True)
root.attributes('-topmost', True)
root.attributes('-transparent', True)  # macOS supports this
root.attributes('-alpha', 0.5)         # 1.0 = fully opaque, 0.0 = fully transparent

w = root.winfo_screenwidth()
h = root.winfo_screenheight()

canvas = tk.Canvas(root, width=w, height=h, bg='black', highlightthickness=0)
canvas.pack()

img = Image.open("spider.png").convert("RGBA")
spider_img = ImageTk.PhotoImage(img)

def add_spider():
    x = random.randint(0, w)
    y = random.randint(0, h)
    canvas.create_image(x, y, image=spider_img)
    root.after(500, add_spider)

root.protocol('WM_DELETE_WINDOW', lambda: None)
add_spider()
root.mainloop()

⚠️ Note

  • macOS transparency (-transparent) only works properly on Tk 8.6.13+.
  • You may not get full transparency depending on your Tk version and system theme.
  • If you want to simulate transparency, ensure your spider image has a transparent background (use .convert("RGBA")).

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages