使用qprocess在gui中启动脚本工作在3.4而不是2.7挂在raw_input上?

时间:2017-01-09 20:43:57

标签: python python-2.7 user-interface pyqt qprocess

我在anaconda IDE和python 3.4中完成了所有gui应用程序的开发,并使用一个简单的测试脚本,我认为它在3.4显示输出实时并等待输入时运行良好。该应用程序的最终目标是运行一个我无法控制的脚本,并在2.7中编码,并发送脚本命令。我转换了足够的gui脚本在2.7中工作,但qprocess没有按预期工作。假设它挂在raw_input上,使用简单的测试脚本就不会显示任何内容。当我运行所需的脚本时,不显示任何内容,然后显示所有内容,并显示已关闭的消息(可能它具有超时功能无法检查)。遗憾的是,我无法发布实际的脚本,但我已经将代码简化为这个对我来说不起作用的简单测试用例。我认为问题不是实时读取脚本输出,而是没有正确等待输入的过程。我怎样才能在2.7中使用它?

mainwindow.ui:

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>MainWindow</class>
 <widget class="QMainWindow" name="MainWindow">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>562</width>
    <height>608</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralWidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <layout class="QHBoxLayout" name="horizontalLayout">
      <item>
       <widget class="QLineEdit" name="fileLocation"/>
      </item>
      <item>
       <widget class="QPushButton" name="openFileButton">
        <property name="text">
         <string>OpenFile</string>
        </property>
       </widget>
      </item>
      <item>
       <widget class="QPushButton" name="startButton">
        <property name="text">
         <string>Start</string>
        </property>
       </widget>
      </item>
     </layout>
    </item>
    <item>
     <widget class="QTextBrowser" name="textBrowser"/>
    </item>
   </layout>
  </widget>
  <widget class="QMenuBar" name="menuBar">
   <property name="geometry">
    <rect>
     <x>0</x>
     <y>0</y>
     <width>562</width>
     <height>21</height>
    </rect>
   </property>
  </widget>
  <widget class="QToolBar" name="mainToolBar">
   <attribute name="toolBarArea">
    <enum>TopToolBarArea</enum>
   </attribute>
   <attribute name="toolBarBreak">
    <bool>false</bool>
   </attribute>
  </widget>
  <widget class="QStatusBar" name="statusBar"/>
 </widget>
 <layoutdefault spacing="6" margin="11"/>
 <resources/>
 <connections/>
</ui>

main.py:

from PyQt4 import QtCore, QtGui, uic

class MyMainWindow(QtGui.QMainWindow):
    def __init__(self, parent=None):
        QtGui.QMainWindow.__init__(self, parent)
        uic.loadUi('mainwindow.ui', self)
        self.openFileButton.clicked.connect(self.getFname)
        self.process=QtCore.QProcess(self)
        self.process.readyRead.connect(self.dataReady)
        self.startButton.clicked.connect(self.startScript)

    def getFname (self):
        self.fileLocation.setText(QtGui.QFileDialog.getOpenFileName(self, "Save File", "", "*.py ;; All files *.*"))

    def dataReady(self):
        self.textBrowser.append(bytearray(self.process.readAllStandardOutput()).decode('utf-8'))

    def startScript(self):
        self.process.start('python',[self.fileLocation.text()])

if __name__ == "__main__":
    import sys
    app = QtGui.QApplication(sys.argv)
    main_window = MyMainWindow()
    main_window.show()
    sys.exit(app.exec_())  

为python 2.7编写的test.py:

print "running script 2" 
print "Enter an input A,B,C:"

s=raw_input("")
print "you selected:"+s

1 个答案:

答案 0 :(得分:0)

脚本的Python版本可能与GUI的版本不同,因为您正在启动外部流程。因此,您可以在Python 3.4中运行GUI,并且脚本在Python 2.7中运行。只需确保指定要使用的Python可执行文件的完整路径。