GUI noob here. I've created a simple screen overlay where the user can click-drag to create a transparent rectangle.
Think 'cropping an image'.
That part was easy enough. The problem is that I want to reset the overlay if/when the user attempts to click-drag inside the transparent rectangle, thereby starting a new rectangle.
Illustrative example code:
import tkinter as tk
def fail_click(evt):
print(evt.x)
root = tk.Tk()
root.attributes('-topmost', True)
root.attributes('-transparentcolor', 'grey')
canvas = tk.Canvas(root, width=300, height=300, bg='grey')
canvas.bind('<Button-1>', fail_click)
canvas.pack()
root.mainloop()
What I expected: An invisible canvas upon which I could draw.
What I got: An empty frame I could reach through to grab whatever is on the other side. This surprised me because I was unaware that 'transparent' == 'not there'. While I can imagine cool things to do with this newfound knowledge, in this case, it's the opposite of what I want.
The question(s):
Is this the intended behavior for GUI windows in general or is this a quirk of Tkinter? In other words, could I solve this problem simply by using a different toolkit, e.g., wxPython or pyQt?
Assuming this is the intended behavior, is there a workaround for getting my invisible canvas to register mouse events. e.g., capturing the click event and redirecting it to where it belongs (the canvas)? It doesn't need to be trivial, I just need a signpost pointing me in the right direction.
Notes:
- I haven't put a lot of effort into this as it's just a silly side project I'm working on. I just decided to ask the question before I waste time I don't have trying a hundred different things, like learning how to use a new toolkit. And, who knows, maybe it'll help save someone else some time.
- I've glanced over the code from this answer - https://stackoverflow.com/a/42880450/3602761 - but that's just capturing click events. I need the whole caboodle: Motion, Button-1, B1-Motion, ButtonRelease-1, etc.
- I'm only using Python and Tkinter here because Python is the language I'm most familiar with and Tkinter is built-in. Feel free to offer suggestions straying from this implementation decision.