How to make click-through windows PyQt

你。 提交于 2019-12-04 13:37:10

Here is a solution on Windows using PyQt4.

You need to override the eventFilter in the Front widget (on Windows it is winEvent) and then forward the events to the Back window.

I'm not completely sure, but there must be a similar approach that can be used on other platforms (instead of winEvent, maybe x11Event?)

Good luck!

from PyQt4 import QtCore, QtGui
import win32api, win32con, win32gui, win32ui

class Front(QtGui.QPushButton):
    def __init__(self,text="",whndl=None):
        super(Front,self).__init__(text)
        self.pycwnd = win32ui.CreateWindowFromHandle(whndl)

    # install an event filter for Windows' messages. Forward messages to 
    # the other HWND
    def winEvent(self,MSG):

        # forward Left button down message to the other window.  Not sure 
        # what you want to do exactly, so I'm only showing a left button click.  You could 
        if MSG.message == win32con.WM_LBUTTONDOWN or \
           MSG.message == win32con.WM_LBUTTONUP:

            print "left click in front window"
            self.pycwnd.SendMessage(MSG.message, MSG.wParam, MSG.lParam)
            return True, 0 # tells Qt to ignore the message

        return super(Front,self).winEvent(MSG)

class Back(QtGui.QPushButton):
    def __init__(self,text=""):
        super(Back,self).__init__(text)
        self.clicked.connect(self.onClick)

    def onClick(self):
        print 'back has been clicked'

def main():
    a = QtGui.QApplication([])

    back = Back("I'm in back...")
    back.setWindowTitle("I'm in back...")
    back.show()

    # Get the HWND of the window in back (You need to use the exact title of that window)
    whndl = win32gui.FindWindowEx(0, 0, None, "I'm in back...")

    # I'm just making the front button bigger so that it is obvious it is in front ...
    front = Front(text="*____________________________*",whndl=whndl)
    front.setWindowOpacity(0.8)
    front.show()    

    a.exec_()

if __name__ == "__main__":
    main()
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!