skip to Main Content

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.
enter image description here
And just so the terminal is clear folder git status environment laptop battery

2

Answers


  1. Chosen as BEST ANSWER

    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 adding root.update_idletasks() before root.attributes("-alpha", 0.5) my script now behaves like the terminal.

    The updated code is now:

    import tkinter as tk
    
    if __name__ == "__main__":
        root = tk.Tk()
        root.geometry("400x400")
        root.update_idletasks()
        root.attributes("-alpha", 0.5)
        root.mainloop()
    

    Thanks for the help! I am leaving the answer here in case someone faces the same issue in the future.


  2. 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 with root.after

    import tkinter as tk
    
    def set_transparency():
        root.attributes("-alpha", 0.5)
    
    if __name__ == "__main__":
        root = tk.Tk()
        root.geometry("400x400")
        root.after(0, set_transparency)  # Ensure this line
        root.mainloop()
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search