In tkinter it is possible to create a 'Toplevel' window, and allow it to be transparent using the following code:
self._window = tkinter.Toplevel(self._tk_master)
self._window.wait_visibility()
self._window.wm_attributes('-alpha', 0.4)
self._canvas = tkinter.Canvas(self._window, width=self._width, height=self._height)
self._canvas.pack()
Now my question is how to create a non transparent rectangle with black background on the new transparent window?
I tried just creating a rectangle and set the 'fill' to black, but the rectangle seems to inherit the window transparency:
self._background_rectangle = self._canvas.create_rectangle(
0,
0,
self._width,
self._height,
fill="black")
I tried to create an image with 'RGBA' and set the transparency, but it seems to have no effect on the UI displayed:
alpha = 0.99
fill = tk_master.winfo_rgb(fill) + (int(alpha * 255),)
image_object = PIL.Image.new("RGBA", (width, height), fill)
image_object = PIL.ImageTk.PhotoImage(image_object)
self._canvas.create_image(x, y, image=image_object, anchor=tkinter.NW)
I also took an image, that contains only black background, and tried to add it, and still the image does not show!
image_object = PIL.Image.open(image_with_only_black_background)
image_object.resize((width, height), PIL.Image.ANTIALIAS)
image_object = PIL.ImageTk.PhotoImage(image_object)
self._canvas.create_image(x, y, image=image_object, anchor=tkinter.NW)
Does someone know how to solve this? Or what am I doing wrong?
Thanks!