简体   繁体   English

如何使用 python 使点击事件通过 window?

[英]how could I make click events pass through a window using python?

I want to add overlays to my screen that displays some shapes using python and I am trying to achieve this by making a window transparent and make click events pass through the window.我想使用 python 向我的屏幕添加显示一些形状的叠加层,我试图通过使 window 透明并使点击事件通过 window 来实现这一点。 I am using python 3.6 (i can change the version if neccesary) and I am on windows 10.我正在使用 python 3.6(如果需要,我可以更改版本)并且我在 windows 10 上。

Note: similar questions have been asked in the past here and here but neither answer my question.注意:过去在这里这里都提出过类似的问题,但都没有回答我的问题。

Thanks in advance提前致谢

Tkinter does not have passing events to parent widgets. Tkinter 没有将事件传递给父小部件。 But it is possible to simulate the effect through the use of bindtags但是可以通过使用bindtags来模拟效果

Making a binding to a widget is not equal to adding a binding to a widget.绑定到小部件不等于添加绑定到小部件。 It is binding to a bindtag which has the same name as the widget, but it's not actually the widget.它绑定到一个与小部件同名的绑定bindtag ,但它实际上不是小部件。

Widgets have a list of bindtags , when an event happens on a widget, the bindings for each tag are processed.小部件有一个bindtags列表,当小部件上发生事件时,会处理每个标签的绑定。

import Tkinter as tk

class BuildCanvas(tk.Frame):
    def __init__(self, parent):
        tk.Frame.__init__(self, parent)

        self.main = tk.Canvas(self, width=250, height=250, 
                              borderwidth=0, highlightthickness=0,
                              background="linen")
        self.main.pack(side="top", fill="both", expand=True)

        self.main.bind("<1>", self.on_main_click)

        for x in range(10):
            for y in range(10):
                canvas = tk.Canvas(self.main, width=35, height=35, 
                                   borderwidth=1, highlightthickness=0,
                                   relief="groove")
                if ((x+y)%2 == 0):
                    canvas.configure(bg="yellow")

                self.main.create_window(x*45, y*45, anchor="nw", window=canvas)

                bindtags = list(canvas.bindtags())
                bindtags.insert(1, self.main)
                canvas.bindtags(tuple(bindtags))
                canvas.bind("<1>", self.on_sub_click)


    def on_sub_click(self, event):
        print("sub-canvas binding")
        if event.widget.cget("background") == "yellow":
            return "break"

    def on_main_click(self, event):
        print("main widget binding")

if __name__ == "__main__":
    root = tk.Tk()
    BuildCanvas(root).pack (fill="both", expand=True)
    root.mainloop()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM