How can I get an external program in QtWidget using PyQt5 and pywin32?

Asked 2 years ago, Updated 2 years ago, 92 views

Hello We are developing GUI programs using PyQt5.

I want to implement the ability to subordinate external programs to follow.

When I searched, C# said that I can call the setParent function, win32gui.There is an error when calling SetParent.
The error content is pywintypes.error: (87, 'SetParent', 'parameters incorrect.') .
Therefore, we used ctypes.windll.user32.SetParent() using ctypes, but the error did not occur, but it did not work properly.

I searched yesterday evening and until dawn, but I don't know how to solve it.

I would appreciate it if you could help me if you know how to attach a different window to the window created by PyQt or what I'm overlooking

The code I wrote about SetParent is as follows.

if __name__ == '__main__':
    # Class created using pywin32 library
    wh = WindowHelper()

    app = QtWidgets.QApplication(sys.argv)
    Dialog = QtWidgets.QDialog()

    ui = Ui_Dialog()
    ui.setupUi(Dialog)
    Dialog.setWindowFlag(QtCore.Qt.WindowMinimizeButtonHint)
    Dialog.show()

    # # SetParent
    main = Dialog.winId()
    hWnd = wh.getWindow (None, "Calculator")

    # Calculator does not move along the main window
    ctypes.windll.user32.SetParent(hWnd, int(main))
    # Error
    win32gui.SetParent(int(main), hWnd)

python pyqt5 pywin32

2022-09-22 11:59

1 Answers

Please refer to the code below.

import sys
from PyQt5.QtWidgets import QApplication, QWidget
from ctypes import *

class App(QWidget):
    def __init__(self):
        super().__init__()
        self.left=50
        self.top=50
        self.width=1200
        self.height=800
        self.initUI()

    def initUI(self):
        self.setGeometry(self.left,self.top,self.width,self.height)
        self.show()

if __name__=='__main__':
    FindWindow = windll.user32.FindWindowW
    SetParent = windll.user32.SetParent
    SetWindowPos = windll.user32.SetWindowPos

    notepad_handle = FindWindow (0, "No Title - Windows Notebook")

    app = QApplication(sys.argv)
    ex = App()
    SetParent(notepad_handle, int(ex.winId()))
    SetWindowPos(notepad_handle, 0, 100, 100, 400, 600, 0)

    sys.exit(app.exec_())


2022-09-22 11:59

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.