Search code examples
pythonuser-interfacetkinterwindow

How to create a translucent window in tinter?


I am trying to create a translucent window in Tkinter like the one in windows 11 enter image description here

How to do this? If we cannot do this can we capture a part of a screen and blur it using cv2 and use it as a continuously updating background?


Solution

  • No, this is not directly possible with Tkinter. But:

    If you use PIL, you can get the location of the window, and then take a screenshot, then blur it and then make it your app background. But this won't work if user tries to move/resize the application. But here is a rough code:

    from tkinter import *
    from PIL import ImageTk, ImageGrab, ImageFilter # pip install Pillow
    
    root = Tk()
    root.overrideredirect(1) # Hide the titlebar etc..
    
    bg = Canvas(root)
    bg.pack(fill='both',expand=1)
    root.update()
    
    # Get required size and then add pixels to remove title bar and window shadow
    left   = root.winfo_rootx()
    top    = root.winfo_rooty()
    right  = left + root.winfo_width()
    bottom = top  + root.winfo_height()
    
    root.withdraw() # Hide the window
    img = ImageGrab.grab((left,top,right,bottom)) # Get the bg image
    root.deiconify() # Show the window
    
    img = img.filter(ImageFilter.GaussianBlur(radius=5)) # Blur it 
    img = ImageTk.PhotoImage(img)
    bg.create_image(0,0, image=img, anchor='nw') # Show in canvas
    
    label = Label(root,text='This is a translucent looking app')
    bg.create_window(bg.winfo_width()/2,bg.winfo_height()/2,window=label) # Position in the center
    
    root.mainloop()
    

    Output with Tkinter:


    Tkinter is not the best choice if you are trying to go for a modern look, use PyQt and check qtacrylic

    Output with PyQt: