PyQT:将键击发送到进程的stdin

时间:2012-10-02 20:26:56

标签: python qt process pyqt

我正在尝试将每个击键类型发送到使用QProcess的进程运行,但这些字符似乎没有通过。 (快速入侵)代码是:


#!/usr/bin/env python
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'q4testgui.ui'
#
# Created: Fri Sep 21 11:53:03 2012
#      by: PyQt4 UI code generator 4.9.1
#
# WARNING! All changes made in this file will be lost!

import sys
import re
from subprocess import Popen, PIPE
from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    _fromUtf8 = lambda s: s

class Ui_MainWindow(QtGui.QMainWindow):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(788, 664)
        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.runBdiTestBtn = QtGui.QPushButton(self.centralwidget)
        self.runBdiTestBtn.setGeometry(QtCore.QRect(20, 10, 161, 61))
        self.runBdiTestBtn.setToolTip(_fromUtf8("Run BDI tests.  Ensure the BDI and ethernet cable are connected to the Talisman."))
        self.runBdiTestBtn.setStatusTip(_fromUtf8(""))
        self.runBdiTestBtn.setObjectName(_fromUtf8("runBdiTestBtn"))
        self.textEdit = QtGui.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 350, 751, 281))
        self.textEdit.setToolTip(_fromUtf8(""))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.label = QtGui.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 330, 64, 17))
        self.label.setObjectName(_fromUtf8("label"))
        self.clearBtn = QtGui.QPushButton(self.centralwidget)
        self.clearBtn.setGeometry(QtCore.QRect(80, 320, 81, 28))
        self.clearBtn.setObjectName(_fromUtf8("clearBtn"))
        self.setEnvBtn = QtGui.QPushButton(self.centralwidget)
        self.setEnvBtn.setGeometry(QtCore.QRect(20, 250, 161, 61))
        self.setEnvBtn.setToolTip(_fromUtf8("Set environment variables on the Talisman."))
        self.setEnvBtn.setStatusTip(_fromUtf8(""))
        self.setEnvBtn.setObjectName(_fromUtf8("setEnvBtn"))
        self.eth0Addr1 = QtGui.QLineEdit(self.centralwidget)
        self.eth0Addr1.setGeometry(QtCore.QRect(270, 250, 31, 27))
        self.eth0Addr1.setToolTip(_fromUtf8(""))
        self.eth0Addr1.setObjectName(_fromUtf8("eth0Addr1"))
        self.label_3 = QtGui.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(190, 250, 71, 29))
        self.label_3.setObjectName(_fromUtf8("label_3"))
        self.label_4 = QtGui.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(190, 280, 71, 29))
        self.label_4.setObjectName(_fromUtf8("label_4"))
        self.eth0Addr2 = QtGui.QLineEdit(self.centralwidget)
        self.eth0Addr2.setGeometry(QtCore.QRect(310, 250, 31, 27))
        self.eth0Addr2.setToolTip(_fromUtf8(""))
        self.eth0Addr2.setObjectName(_fromUtf8("eth0Addr2"))
        self.eth0Addr3 = QtGui.QLineEdit(self.centralwidget)
        self.eth0Addr3.setGeometry(QtCore.QRect(350, 250, 31, 27))
        self.eth0Addr3.setToolTip(_fromUtf8(""))
        self.eth0Addr3.setObjectName(_fromUtf8("eth0Addr3"))
        self.eth0Addr4 = QtGui.QLineEdit(self.centralwidget)
        self.eth0Addr4.setGeometry(QtCore.QRect(390, 250, 31, 27))
        self.eth0Addr4.setToolTip(_fromUtf8(""))
        self.eth0Addr4.setText(_fromUtf8(""))
        self.eth0Addr4.setObjectName(_fromUtf8("eth0Addr4"))
        self.eth0Addr5 = QtGui.QLineEdit(self.centralwidget)
        self.eth0Addr5.setGeometry(QtCore.QRect(430, 250, 31, 27))
        self.eth0Addr5.setToolTip(_fromUtf8(""))
        self.eth0Addr5.setText(_fromUtf8(""))
        self.eth0Addr5.setObjectName(_fromUtf8("eth0Addr5"))
        self.eth0Addr6 = QtGui.QLineEdit(self.centralwidget)
        self.eth0Addr6.setGeometry(QtCore.QRect(470, 250, 31, 27))
        self.eth0Addr6.setToolTip(_fromUtf8(""))
        self.eth0Addr6.setText(_fromUtf8(""))
        self.eth0Addr6.setObjectName(_fromUtf8("eth0Addr6"))
        self.eth1Addr5 = QtGui.QLineEdit(self.centralwidget)
        self.eth1Addr5.setGeometry(QtCore.QRect(430, 280, 31, 27))
        self.eth1Addr5.setToolTip(_fromUtf8(""))
        self.eth1Addr5.setText(_fromUtf8(""))
        self.eth1Addr5.setObjectName(_fromUtf8("eth1Addr5"))
        self.eth1Addr4 = QtGui.QLineEdit(self.centralwidget)
        self.eth1Addr4.setGeometry(QtCore.QRect(390, 280, 31, 27))
        self.eth1Addr4.setToolTip(_fromUtf8(""))
        self.eth1Addr4.setText(_fromUtf8(""))
        self.eth1Addr4.setObjectName(_fromUtf8("eth1Addr4"))
        self.eth1Addr3 = QtGui.QLineEdit(self.centralwidget)
        self.eth1Addr3.setGeometry(QtCore.QRect(350, 280, 31, 27))
        self.eth1Addr3.setToolTip(_fromUtf8(""))
        self.eth1Addr3.setObjectName(_fromUtf8("eth1Addr3"))
        self.eth1Addr6 = QtGui.QLineEdit(self.centralwidget)
        self.eth1Addr6.setGeometry(QtCore.QRect(470, 280, 31, 27))
        self.eth1Addr6.setToolTip(_fromUtf8(""))
        self.eth1Addr6.setText(_fromUtf8(""))
        self.eth1Addr6.setObjectName(_fromUtf8("eth1Addr6"))
        self.eth1Addr2 = QtGui.QLineEdit(self.centralwidget)
        self.eth1Addr2.setGeometry(QtCore.QRect(310, 280, 31, 27))
        self.eth1Addr2.setToolTip(_fromUtf8(""))
        self.eth1Addr2.setObjectName(_fromUtf8("eth1Addr2"))
        self.eth1Addr1 = QtGui.QLineEdit(self.centralwidget)
        self.eth1Addr1.setGeometry(QtCore.QRect(270, 280, 31, 27))
        self.eth1Addr1.setToolTip(_fromUtf8(""))
        self.eth1Addr1.setObjectName(_fromUtf8("eth1Addr1"))
        self.runUbootTestsBtn = QtGui.QPushButton(self.centralwidget)
        self.runUbootTestsBtn.setGeometry(QtCore.QRect(20, 80, 161, 61))
        self.runUbootTestsBtn.setToolTip(_fromUtf8("Run u-boot tests.  Ensure a serial cable is connected from the test PC to the Talisman.\nThese tests take approx. 10 minutes to run."))
        self.runUbootTestsBtn.setStatusTip(_fromUtf8(""))
        self.runUbootTestsBtn.setObjectName(_fromUtf8("runUbootTestsBtn"))
        self.runLinuxTestsBtn = QtGui.QPushButton(self.centralwidget)
        self.runLinuxTestsBtn.setGeometry(QtCore.QRect(20, 150, 161, 61))
        self.runLinuxTestsBtn.setToolTip(_fromUtf8("Run Linux tests.  Ensure both ethernet cables are connected to the Talisman."))
        self.runLinuxTestsBtn.setStatusTip(_fromUtf8(""))
        self.runLinuxTestsBtn.setObjectName(_fromUtf8("runLinuxTestsBtn"))
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtGui.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 788, 25))
        self.menubar.setObjectName(_fromUtf8("menubar"))
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtGui.QStatusBar(MainWindow)
        self.statusbar.setObjectName(_fromUtf8("statusbar"))
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Talisman Test Suite", None, QtGui.QApplication.UnicodeUTF8))
        self.runBdiTestBtn.setText(QtGui.QApplication.translate("MainWindow", "Run BDI Tests", None, QtGui.QApplication.UnicodeUTF8))
        self.label.setText(QtGui.QApplication.translate("MainWindow", "Output", None, QtGui.QApplication.UnicodeUTF8))
        self.setEnvBtn.setText(QtGui.QApplication.translate("MainWindow", "Set Env. Variables", None, QtGui.QApplication.UnicodeUTF8))
        self.eth0Addr1.setText(QtGui.QApplication.translate("MainWindow", "3c", None, QtGui.QApplication.UnicodeUTF8))
        self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Eth 0 MAC", None, QtGui.QApplication.UnicodeUTF8))
        self.label_4.setText(QtGui.QApplication.translate("MainWindow", "Eth 1 MAC", None, QtGui.QApplication.UnicodeUTF8))
        self.eth0Addr2.setText(QtGui.QApplication.translate("MainWindow", "98", None, QtGui.QApplication.UnicodeUTF8))
        self.eth0Addr3.setText(QtGui.QApplication.translate("MainWindow", "bf", None, QtGui.QApplication.UnicodeUTF8))
        self.eth1Addr3.setText(QtGui.QApplication.translate("MainWindow", "bf", None, QtGui.QApplication.UnicodeUTF8))
        self.eth1Addr2.setText(QtGui.QApplication.translate("MainWindow", "98", None, QtGui.QApplication.UnicodeUTF8))
        self.eth1Addr1.setText(QtGui.QApplication.translate("MainWindow", "3c", None, QtGui.QApplication.UnicodeUTF8))
        self.runUbootTestsBtn.setText(QtGui.QApplication.translate("MainWindow", "Run UBoot Tests", None, QtGui.QApplication.UnicodeUTF8))
        self.runLinuxTestsBtn.setText(QtGui.QApplication.translate("MainWindow", "Run Linux Tests", None, QtGui.QApplication.UnicodeUTF8))
        self.clearBtn.setText(QtGui.QApplication.translate("MainWindow", "Clear", None, QtGui.QApplication.UnicodeUTF8))

    def __init__(self, parent=None):
        QtGui.QWidget.__init__(self, parent)
        self.setupUi(self)
        self.process = QtCore.QProcess(self)
        self.connect(self.runBdiTestBtn, QtCore.SIGNAL('clicked()'), self.runBdiTests)
        self.connect(self.runUbootTestsBtn, QtCore.SIGNAL('clicked()'), self.runUbootTests)
        self.connect(self.runLinuxTestsBtn, QtCore.SIGNAL('clicked()'), self.runLinuxTests)
        self.connect(self.clearBtn, QtCore.SIGNAL('clicked()'), self.clear)

        self.textEdit.installEventFilter(self)

    def runBdiTests(self):
        self.process.readyReadStandardOutput.connect(self.readReady)
        self.process.start("make", ("clean", "check_bdi"))

    def runUbootTests(self):
        self.process.readyReadStandardOutput.connect(self.readReady)
        self.process.start("make", ("clean", "check_uboot"))

    def runLinuxTests(self):
        self.process.readyReadStandardOutput.connect(self.readReady)
        self.process.start("make", ("clean", "check_linux"))

    def clear(self):
        self.textEdit.clear()

    # Handler for reading stdout from test process
    def readReady(self):
        chunk = self.process.readAllStandardOutput()
        self.textEdit.append(str(chunk).rstrip())
        self.textEdit.moveCursor(QtGui.QTextCursor.End) 
        print chunk,

    # Event handler for intercepting keystrokes and sending them to stdin of the test process
    def eventFilter(self, source, event):
        if (event.type() == QtCore.QEvent.KeyPress):
            self.textEdit.insertPlainText(str(event.text()))
            self.process.writeData(event.text())
            return True
        else:
            return QtGui.QMainWindow.eventFilter(self, source, event)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    myapp = Ui_MainWindow()
    myapp.show()
    sys.exit(app.exec_())

0 个答案:

没有答案