'Tkinter clicking through windows erases invisible background properties from canvas

enter image description here

I can click through the image of the canvas,the red circle, but the black background of the canvas use to be transparent before. I want to keep the background transparent and just see the img (The red circle)

This is the function I found on StackOverflow to click through a tkinter windows

        print("setting window properties")
        try:
            styles = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
            styles = win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT
            win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, styles)
            win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA)
        except Exception as e:
            print(e)

This is so far my code.

import time
import pyautogui
pyautogui. FAILSAFE = False

import win32gui
import win32con

class Interface:
    def __init__(self):
        self.top = None
        self.canvas = None
        self.img1 = None

    def set_imgs(self):
        self.canvas = Canvas(self.top,width= 1000, height=1200,  bd=0, highlightthickness=0)
        self.setClickthrough(self.canvas.winfo_id())
        self.img1 = PhotoImage(file="cursor.png")
        
    def create_interface(self):
        self.top.configure(background='black')
        self.top.wm_attributes("-topmost", 1)
        self.top.overrideredirect(1)
        self.top.wm_attributes("-transparentcolor", "black")
        self.top.geometry("1920x1080")
        self.canvas.pack()
        self.canvas.create_image(500, 540, image=self.img1, anchor=CENTER)
        self.canvas.configure(bg='black')
    
    
    def update_geometry(self,x=0,y=0):
        self.top.geometry(f"1800x1000+{x}+{y}")

    
    def setClickthrough(self,hwnd):
        print("setting window properties")
        try:
            styles = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
            styles = win32con.WS_EX_LAYERED | win32con.WS_EX_TRANSPARENT
            win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE, styles)
            win32gui.SetLayeredWindowAttributes(hwnd, 0, 255, win32con.LWA_ALPHA)
        except Exception as e:
            print(e)
    
   
def main():
    
    an_interface = Interface()
    an_interface.top = Tk()
    
    
    an_interface.set_imgs()
    an_interface.create_interface()

    an_interface.top.mainloop()
if __name__=='__main__':
    main()


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source