launching script within gui with qprocess works in 3.4 not 2.7 hanging on raw_input?

146 views Asked by At

I did all of the developing of a gui application in the anaconda IDE and in python 3.4 and using a simple test script I thought it was working well within 3.4 displaying output in realtime and waiting for input. The end goal of the application was to run a script, that I have no control over and is coded in 2.7, and send the script commands. I converted enough of the gui script to work in 2.7 but qprocess doesn't work as expected. With the simple test script nothing at all is displayed, assuming its hanging at raw_input. When I run the desired script nothing is displayed then everything is displayed with the message showing it has shut down (maybe it has timeout function can't check). Unfortunately I can't post the actual script but I have boiled the code down to this simple test case that doesn't function for me. I think the issue is not reading the script output in realtime and the process not properly waiting for input. How can I get this working in 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_())  

test.py coded for python 2.7:

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

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

There are 1 answers

3
Oliver On

The Python version of the script can be different from that of the GUI because you are starting an external process. So you can have the GUI run in Python 3.4, and the script run in Python 2.7. Just ensure that you specify the full path to the Python executable that you want to use.