0

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
kaminsknator
  • 1,135
  • 3
  • 15
  • 26
  • can you post the 3.4 script that worked? – Jean-François Fabre Jan 09 '17 at 21:04
  • yes: `print ("running script 2") print ("Enter an input A,B,C:") s=input("") print ("you selected:"+s)` – kaminsknator Jan 09 '17 at 21:36
  • can you try `print ("running script 2") print ("Enter an input A,B,C:") s=str(input("")) print ("you selected:"+s)` in your python 2.7 test? – Jean-François Fabre Jan 09 '17 at 21:39
  • 1
    What is the relevance of the ui file and all the encoding junk in the main.py script? Please read the guidance on how to provide a [mcve]. – ekhumoro Jan 09 '17 at 21:40
  • @ekhumoro I included the ui file so that people could run the code without doing any work. If you try to run the code without the ui file you'll just encounter a number of errors and not get a working example – kaminsknator Jan 09 '17 at 21:47
  • @Jean-FrançoisFabre print("") isn't a valid command in python2.7 – kaminsknator Jan 09 '17 at 21:49
  • @Jean-FrançoisFabre you're correct I was confusing the print differences bettween 2.7/3.4 trying the given code produces no output in the textBrowser – kaminsknator Jan 09 '17 at 21:59
  • @kaminsknator. The code you've posted is not in any way minimal - most of the gui elements are not relevant and should be removed. Also, it seems the problem cannot be reproduced without running the code in a very specific environment (i.e. the Anaconda IDE). – ekhumoro Jan 09 '17 at 22:05
  • @ekhumoro I'm not running in Anaconda, it's running in python 2.7 a base python install. I developed in the Anaconda IDE. I'm unaware if the encoding is necessary or not but you wouldn't be able to generate a GUI without the UI file. I'll test with those encoding lines removed though it hardly distracts from the issue. I have replicated it only different systems when troubleshooting. --Removed encoding lines, removing code is succinct as possible. – kaminsknator Jan 09 '17 at 22:08
  • @kaminsknator. Are you claiming that the current example code in your question works differently when run with python2 *vs.* python3? Because it looks very much like it will just block the gui no matter what (if you run it from an ordinary console, that is). Of course, if you tested the original code in an IDE/debugger, the behaviour may well have been different. – ekhumoro Jan 09 '17 at 23:05
  • @kaminsknator 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. Did you try that? – Oliver Jan 09 '17 at 23:10
  • @Schollii I have not tried that but I will give it a go! – kaminsknator Jan 10 '17 at 14:42
  • @ekhumoro When I run it in 3.4 I see output then it waits for input. In 2.7 I don't see anything. – kaminsknator Jan 10 '17 at 14:43

1 Answers1

0

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.

Oliver
  • 27,510
  • 9
  • 72
  • 103
  • This doesn't fix my problem. – kaminsknator Jan 10 '17 at 20:45
  • It is 100% certain that this will work, there is no reason to convert the GUI script. However, there may be other issues with the way you tried to implement what I describe, or just other issues independent of the above. I suggest you run 3.4 app to exec 3.4 script and verify it runs; then replace the 3.4 script with a 2.7 script, keeping the 3.4 app. If that doesn't work, post a new question with that code. – Oliver Jan 10 '17 at 22:55
  • Even that simple test code shown above with the main script ran in 3.4 and the test launched subprocess in 2.7 it still provides no output. – kaminsknator Jan 12 '17 at 15:25