Tkinter Python transparent background with (not transparent ) visible shape?

Viewed 711

I would like to create a non-visible, transparent background with visible shapes inside that window. I wrote the following code, however as you can see the objects are also transparent, How can I solve this?

#!/usr/bin/env python3

from tkinter import *

window = Tk()
window.wait_visibility(window)
window.wm_attributes('-alpha',0.1)


def drag(event):
    event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)

card = Canvas(window, width=74, height=97, bg='blue')
card.place(x=300, y=600,anchor=CENTER)
card.bind("<B1-Motion>", drag)

another_card = Canvas(window, width=74, height=97, bg='red')
another_card.place(x=600, y=600,anchor=CENTER)
another_card.bind("<B1-Motion>", drag)

window.mainloop()

In other words I don't want my objects to be transparent. I only want screen to be transparent. In future I might add pictures rectangles etc to the window. But I want all of them to visible with a transparent background. Any Help?

My OS: Ubuntu

Note: For all who face the same problem, I changed my Library to WXPython. It have transparent background, Transparent Window, Different shaped windows etc. But if you guys able to find a solution, I would be grateful for the community. In UBUNTU there is no found answer for this problem. Windows and Mac have it tho, BR

2 Answers

I have a workaround and it was working in windows not sure about ubuntu by using a transparent canvas

from tkinter import *

window = Tk()
window.lift()
window.wm_attributes("-topmost", True)
window.wm_attributes("-transparentcolor", 'gray')
window.wait_visibility()

canvas = Canvas(window, width=500, height=500)
canvas.pack()
canvas.config(cursor='tcross')
canvas.create_rectangle(0, 0, 500, 500, fill='gray', outline='gray')

def drag(event):
    event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)

card = Canvas(window, width=74, height=97, bg='blue')
card.place(x=0, y=0,anchor=CENTER)
card.bind("<B1-Motion>", drag)

another_card = Canvas(window, width=74, height=97, bg='red')
another_card.place(x=600, y=600,anchor=CENTER)
another_card.bind("<B1-Motion>", drag)
window.mainloop()

References: https://stackoverflow.com/a/42880450/10849457

Output: Result

Can you change another_card.place(x=100, y=100,anchor=CENTER)

and

call this window.wm_attributes("-transparentcolor", 'gray') later

from tkinter import *

window = Tk()
window.lift()
window.wm_attributes("-topmost", True)
window.wait_visibility()

canvas = Canvas(window, width=500, height=500)
canvas.pack()
canvas.config(cursor='tcross')
canvas.create_rectangle(0, 0, 500, 500, fill='gray', outline='gray')

def drag(event):
    event.widget.place(x=event.x_root, y=event.y_root,anchor=CENTER)

card = Canvas(window, width=74, height=97, bg='blue')
card.place(x=0, y=0,anchor=CENTER)
card.bind("<B1-Motion>", drag)

another_card = Canvas(window, width=74, height=97, bg='red')
another_card.place(x=100, y=100,anchor=CENTER)
another_card.bind("<B1-Motion>", drag)
window.wm_attributes("-transparentcolor", 'gray')
window.mainloop()
Related