# 🕷️ 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()- The program opens a Tkinter fullscreen window.
- The window stays always on top of other apps.
- The
-transparentcolorattribute makes all black areas invisible (Windows only). - A spider image is loaded from
spider.png. - Every 500 ms, a new spider is drawn at a random location on the screen.
- The window can’t be closed using the normal “X” button, creating a continuous effect.
Install the required library:
pip install pillow-
Place a transparent-background image named
spider.pngin the same folder as your script. -
Run the program:
python main.py
-
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.
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.
If you run this program on macOS, you’ll likely see the following error:
_tkinter.TclError: bad attribute "-transparentcolor"
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.
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()- 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")).