Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
893 views
in Technique[技术] by (71.8m points)

python - How to embed a cmd to pyqt5 application

I tried codes below, but it seemed not work as normal. I cannot use embedded cmd whatever I did. It looks like decorations. I mean I just want to use the cmd like the ordinary one. Here, I paste the code, and any advice would be greatly appreciated. I develop this app on Python 3.7.3 conda environment, Window10.

import sys
import subprocess
import time
import win32gui

from PyQt5.QtCore import QProcess, Qt
from PyQt5.QtGui import QWindow, QIcon, QFont
from PyQt5.QtWidgets import QMainWindow
from PyQt5.QtWidgets import QMdiArea, QSplitter, QTextBrowser
from PyQt5.QtWidgets import QWidget, QApplication, QVBoxLayout
from win32com import client
from win32gui import GetWindowText, EnumWindows,SetForegroundWindow
class Example(QMainWindow):

    def __init__(self):
        super().__init__()
        self.p = QProcess()
        self.layout = QVBoxLayout()
        self.mdi = QMdiArea()
        self.mainSplitter = QSplitter(Qt.Vertical)
        self.setCentralWidget(self.mainSplitter)
        self.mainSplitter.addWidget(QTextBrowser())
        self.initUI()

    def initUI(self):
        self.runExe()
        EnumWindows(self.set_cmd_to_foreground, None)
        hwnd1 = win32gui.GetForegroundWindow()
        #hwnd1 = win32gui.FindWindow(None, "C:\Windows\system32\calc.exe")
        print(hwnd1)
        window = QWindow.fromWinId(hwnd1)
        container_widge = self.createWindowContainer(window, self)
        container_widge.setFocusPolicy(Qt.TabFocus)
        container_widge.setFocus()
        container_widge.setWindowTitle("ain")
        container_widge.setFont(QFont("Times New Roman"))
        container_widge.setGeometry(500, 500, 450, 400)
        #container_widge.setFocusPolicy()
        container_widge.activateWindow()
        container_widge.acceptDrops()
        container_widge.grabMouse()
        container_widge.setMouseTracking(True)
        self.mainSplitter.addWidget(container_widge)
        self.showMaximized()
        #self.setGeometry(200, 200, 700, 700)
        #self.show()
    def runExe(self):
        shell.run("cmd.exe")
        time.sleep(1)
    def set_cmd_to_foreground(self, hwnd, extra):
        """sets first command prompt to forgeround"""
        if "cmd.exe" in GetWindowText(hwnd):
            print(hwnd)
            SetForegroundWindow(hwnd)
            return
    def run_script(self, shell, scripts):
        """runs the py script"""
        shell.SendKeys(scripts+"{ENTER}")
if __name__ == '__main__':
    shell = client.Dispatch("WScript.Shell")
    app = QApplication(sys.argv)
    ex = Example()
    #ex.run_script(shell, "python -m pip list")
    #ex.show()
    sys.exit(app.exec_())

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...