Tkinter see through window not affected by mouse c

2019-05-30 20:21发布

问题:

I am currently controlling a game with python by sending mouse and keystroke commands. What I am looking to do is have a transparent Tkinter window lay overtop of the game to provide some information such as mouse location and pixel color.

I am familiar with changing the window's alpha attribute to make it transparent but have no idea how to always keep that window in front and have mouse clicks pass through it.

My current method of controlling the game involves taking screenshots in certain locations and analyzing the color content. I will also need some way to do this without the Tkinter window interfering.

Pyscreenshot is used for screenshots win32api is used for clicking

Thank you, Alec

回答1:

you can use the SetWindowLong function of win32gui module. If you want a transparent click through window you have to apply GWL_EXSTYLE's ony our window. Therefore you need the windowhandle of your Window.

    hwnd = win32gui.FindWindow(None, "Your window title") # Getting window handle
    # hwnd = root.winfo_id() getting hwnd with Tkinter windows
    # hwnd = root.GetHandle() getting hwnd with wx windows
    lExStyle = win32gui.GetWindowLong(hwnd, win32con.GWL_EXSTYLE)
    lExStyle |=  win32con.WS_EX_TRANSPARENT | win32con.WS_EX_LAYERED 
    win32gui.SetWindowLong(hwnd, win32con.GWL_EXSTYLE , lExStyle )

If you want to change the transparency of your window via winapi use SetLayeredWindowAttributes.