Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Creating a transparent background in a Tkinter window
Tkinter windows provide built-in methods to create transparent backgrounds by making specific colors invisible. This is achieved using the wm_attributes('-transparentcolor', 'color') method, which makes all pixels of the specified color completely transparent.
Basic Transparent Window
To create a transparent background, set the window's background color and then make that same color transparent ?
# Import the Tkinter Library
from tkinter import *
# Create an instance of Tkinter Frame
win = Tk()
# Set the geometry of window
win.geometry("700x350")
# Add a background color to the Main Window
win.config(bg='#add123')
# Create a transparent window
win.wm_attributes('-transparentcolor', '#add123')
win.mainloop()
This creates a window where the background appears completely transparent, showing whatever is behind it on your desktop.
Transparent Window with Widgets
You can add widgets with different colors that remain visible while the background stays transparent ?
from tkinter import *
# Create main window
win = Tk()
win.geometry("400x300")
win.title("Transparent Window with Widgets")
# Set background color and make it transparent
transparent_color = '#f0f0f0'
win.config(bg=transparent_color)
win.wm_attributes('-transparentcolor', transparent_color)
# Add visible widgets with different colors
label = Label(win, text="This label is visible!",
bg='white', fg='black', font=('Arial', 14))
label.pack(pady=20)
button = Button(win, text="Click Me",
bg='lightblue', fg='black', font=('Arial', 12))
button.pack(pady=10)
win.mainloop()
Key Points
- Only the specified color becomes transparent − other colors remain visible
- Widgets with different background colors will still appear solid
- The transparent color must match exactly (including case and format)
- This feature works on Windows; behavior may vary on other operating systems
Common Use Cases
- Creating overlay applications that sit on top of other windows
- Building desktop widgets with irregular shapes
- Developing screen annotation tools
- Making floating toolbars or notifications
Conclusion
Use wm_attributes('-transparentcolor', 'color') to make specific colors transparent in Tkinter windows. This allows creating overlay applications and irregularly shaped windows while keeping widgets with different colors visible.
