I’m trying to create an interface that has some transparency. I know that should be straightforward, but I’m facing a confusing bug.
If I run the following code in the terminal:
import tkinter as tk
root = tk.Tk()
root.geometry("400x400")
root.attributes("-alpha", 0.5)
root.mainloop()
It works fine, and the window is created perfectly with the dimensions and transparency I requested. However, if I run a script like this (on the same environment):
import tkinter as tk
if __name__ == "__main__":
root = tk.Tk()
root.geometry("400x400")
root.attributes("-alpha", 0.5)
root.mainloop()
It doesn’t work. The dimensions are applied to the new window, but not the transparency.
I tried wm_attributes
and attributes
. Both do not work when running python script.py
.
Am I crazy? Why is there any difference in behaviour?
The environments are the same; I’m using Python 3.13
, and no other dependencies are involved. I’m using Ubuntu 22.04
using x11
.
Edit:
Here is proof I’m running on the same environment.
And just so the terminal is clear folder
git status
environment
laptop battery
2
Answers
So, thanks to the comments, I managed to find an answer. When setting attributes like
-alpha
, some window managers delay applying them until the window is fully realized and shown. By addingroot.update_idletasks()
beforeroot.attributes("-alpha", 0.5)
my script now behaves like the terminal.The updated code is now:
Thanks for the help! I am leaving the answer here in case someone faces the same issue in the future.
Probably: In interactive mode, Tkinter-loop has window-lifecycles full control. The second code has an additional level of encapsulation(
if __name__ == "__main__":
), which may lead to timing issues. Create a function and use withroot.after