简体   繁体   English

在 PyQt 应用程序中运行 Windows 可执行文件(calc.exe 等)

[英]Running a Windows executable (calc.exe, etc) inside a PyQt Application

I'm trying to run a Windows executable inside my application written in Python using PyQt5.我正在尝试在使用 PyQt5 用 Python 编写的应用程序中运行 Windows 可执行文件。 I'm fairly new to using PyQt5 but trying.我对使用 PyQt5 还很陌生,但正在尝试。

The problem I'm facing is actually getting the EXE in question to run within my applications Frame, Window or QWidget.我面临的问题实际上是让有问题的 EXE 在我的应用程序框架、窗口或 QWidget 中运行。

At the moment I don't need to send or get responses/results to and from the Windows executable, I only need the ability to show it within my application.目前,我不需要向和从 Windows 可执行文件发送或获取响应/结果,我只需要能够在我的应用程序中显示它。

class MyWindow(QWidget):

   def __init__(self):
     super().__init__()

     self.initUI()

   def initUI(self):
     # create a process
     exePath = "C:\\Windows\\system32\\calc.exe"
     # subprocess.Popen(exePath)
     os.system(exePath)
     hwnd = win32gui.FindWindow(0,"Calculator")
     print(hwnd)
     time.sleep(0.05)
     window = QtGui.QWindow.fromWinId(hwnd)

     self.createWindowContainer(window, self)
     self.setGeometry(500, 500, 450, 400)
     self.setWindowTitle('File dialog')
     self.show()

def main():
  app = QApplication(sys.argv)
  w = MyWindow()
  sys.exit(app.exec_())


if __name__ == '__main__':
    main()

What I get from the following Code is this:我从以下代码中得到的是:

正在运行的应用程序的屏幕截图

Sorry, I don't know if I understood you correctly, but I suggest trying this:抱歉,我不知道我是否理解正确,但我建议您尝试以下操作:

import sys
import subprocess
import threading
import time
import win32gui
from PyQt5.QtCore    import Qt
from PyQt5.QtGui     import QWindow, QPixmap
from PyQt5.QtWidgets import (QWidget, QApplication, QVBoxLayout,
                             QGridLayout, QMainWindow, QLabel) 


class Example(QMainWindow):
    def __init__(self):
        super().__init__()

        self.central_widget = QWidget()
        self.setCentralWidget(self.central_widget)
        
        self.widget = QWidget()
        self.widget.setFixedSize(230, 280)
        self.widget.setStyleSheet("background-color: yellow;")
        self.label = QLabel(self)
        self.label.setPixmap(
            QPixmap('images/splash_.jpg').scaled(self.size().width()/2, self.size().height())
        )
        
        self.grid_layout = QGridLayout(self.central_widget)
        self.grid_layout.addWidget(self.widget, 0, 0, 2, 1)
        
        self.v_layout = QVBoxLayout(self.widget)
        self.initUI()
        self.grid_layout.addWidget(self.label, 0, 1, 1, 1) 
 
    def initUI(self):
        t = threading.Thread(target=self.runExe)
        t.start()
        #                                                 Calculator
        hwnd1 = win32gui.FindWindowEx(0, 0, "CalcFrame", "Калькулятор")
        start = time.time()
        while hwnd1 == 0:
            time.sleep(0.01)
            #                                                 Calculator 
            hwnd1 = win32gui.FindWindowEx(0, 0, "CalcFrame", "Калькулятор")
            end = time.time()
            if end - start > 5:
                return
        window = QWindow.fromWinId(hwnd1)
        widget = self.createWindowContainer(window, self.central_widget)
        self.v_layout.addWidget(widget)
 
    @staticmethod
    def runExe():
        exePath = "C:/Windows/system32/calc.exe"
        subprocess.Popen(exePath)

 
if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    ex.resize(550, 400)
    ex.show()
    sys.exit(app.exec_())

在此处输入图片说明

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

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