3

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

Meric Ozcan
  • 678
  • 5
  • 25
  • would these be helpful? https://stackoverflow.com/questions/19080499/transparent-background-in-a-tkinter-window and https://stackoverflow.com/questions/18394597/is-there-a-way-to-create-transparent-windows-with-tkinter – a121 Dec 02 '20 at 07:47
  • I am working on Ubuntu. THose questions does not show ubuntu OS solution? – Meric Ozcan Dec 02 '20 at 07:49
  • does this work on your gadget? https://stackoverflow.com/a/18430628/14719340 – a121 Dec 02 '20 at 07:55
  • I edited the question depending on the answer in the link. am I making something wrong? because it does not work objects still transparent?maybe missing something? @a121 – Meric Ozcan Dec 02 '20 at 08:18
  • sorry, I work on windows and so I couldn't verify the previous link's legitimacy. I am unable to do it on windows, that I know. – a121 Dec 02 '20 at 08:27
  • Check out [this](https://stackoverflow.com/a/58381574/13998829) link. – LucioRandy Dec 03 '20 at 16:36
  • @LucioRandy I saw it man. I found a other library wxpython. It have more features. I will try that I belive it can create tranparency. – Meric Ozcan Dec 03 '20 at 18:04
  • [This question](https://stackoverflow.com/questions/63305429/tkinter-set-button-background-transparent-on-ubuntu) may be relevant. – M-Chen-3 Dec 08 '20 at 21:00

2 Answers2

2

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

Vignesh
  • 1,553
  • 1
  • 10
  • 25
0

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()
Aaj Kaal
  • 1,205
  • 1
  • 9
  • 8
  • Can you test my code (https://stackoverflow.com/a/65145544/10849457)in ubuntu if you have Please – Vignesh Dec 08 '20 at 07:37
  • Guys ty but I tested transparentcolor in ubuntu even before your answers. This problem is OS specific. Mac and Windows probably have it. But couldnt find in ubuntu. – Meric Ozcan Dec 08 '20 at 07:51
  • 1
    tkinter.TclError: bad attribute "-transparentcolor": must be -alpha, -topmost, -zoomed, -fullscreen, or -type – Meric Ozcan Dec 08 '20 at 07:53