Bachmann
Bachmann

Reputation: 768

How to make click events pass though a tkinter window?

I have been playing with using tkinter to make overlays so some information would appear on top of the rest of the screen.

I'd rather not have this window block the mouse. Is there any way I can have it so click events pass though my overlay? So the window below my tkinter app receives the event.

Upvotes: 4

Views: 3665

Answers (4)

Nordine Lotfi
Nordine Lotfi

Reputation: 533

There is actually a way to do that, although this only works on Windows (Only tested on Linux and it didn't work there, but I'm not sure for other OSes).

The trick is to use a specific color and make only that color transparent by using the -transparentcolor option, like so:

import tkinter as tk

def make_transparent():
    root = tk.Tk()
    root.title("Transparent Window")

    screen_width = root.winfo_screenwidth()
    screen_height = root.winfo_screenheight()
    root.geometry(f"{screen_width}x{screen_height}")

    frame = tk.Frame(root, bg='white')
    frame.pack(fill=tk.BOTH, expand=True)

    root.wm_attributes('-transparentcolor', 'white')

    root.mainloop()

if __name__ == "__main__":
    make_transparent()

Which makes all parts of the root window where that color is used fully transparent. Any key press or mouse click pass through too.

Moreover, you can even draw on top if you use a Canvas widget, provided you don't draw using the same color you used to make it transparent...

Upvotes: 1

BrainAtom
BrainAtom

Reputation: 29

One way to do this is build your own msg queue with a lightweight caller and scheduler to process msg and state of widget. This class or function is usually there without such name as queue manager because having back-end class is general-purpose item for application. Adding to simple event transfer queue could have this done which is what I am now doing.

Upvotes: 1

Caleb Hulbert
Caleb Hulbert

Reputation: 155

As the other answers stated, I don't think is is possible to do through Tkinter, however, you can use win32 to make the window able to be clicked-through. VRage answered it well in his comment to this post:

Tkinter see through window not affected by mouse clicks

Upvotes: 2

Bryan Oakley
Bryan Oakley

Reputation: 385830

No, there is nothing built in to tkinter that would allow you to do that. The best that you could hope for is to capture an event, then use some sort of platform-specific tool that lets you create new events, and use the coordinates from the captured event to generate a new, non-tkinter event.

Upvotes: 2

Related Questions