Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am having some difficulties to display the output of stdout to a QTextEdit Widget. My GUI does not have only one widget (It has 3 QListWidget and 1 QTableWidget), so I am not using QMainWindow. But the weird thing is that the whole code works perfectly fine if the main.ui file has a QMainWindow with only one widget in it (It prints whatever is in the ai.py file into the QTextEdit).

Right now, the python interpreter is not giving any output when I am running my program at the terminal (so I guess the output is correctly being streamed somewhere) but it is not displaying in my QTextEdit Widget. It's been 1 week and I cannot find where the problem is. I have been coding in python for only a month so please be kind!

main.py file:

def p(x):
    print(x)


class Widget(QtWidgets.QWidget):

    def __init__(self):
        QtWidgets.QWidget.__init__(self)
        self.ui = uic.loadUi('/home/sizon/PycharmProjects/trial/main.ui', self)  
        
        print('Connecting process')
        self.process = QtCore.QProcess(self)
        self.process.readyRead.connect(self.stdoutReady)

        self.process.started.connect(lambda: p('Started!'))
        self.process.finished.connect(lambda: p('Finished!'))

        print('Starting process')
        self.process.start('python', ['/home/sizon/PycharmProjects/trial/ai.py'])
      
    def append(self, text):
        cursor = self.textEdit.textCursor()
        cursor.movePosition(cursor.End)
        cursor.insertText(text)
   

    def stdoutReady(self):
        text = str(self.process.readAllStandardOutput())
        print(text.strip())
        self.append('\n' + text)

    def stderrReady(self):
        text = str(self.process.readAllStandardError())
        self.append(text)

def main():
    import sys
    app = QtWidgets.QApplication(sys.argv)
    Form = Widget()
    Form.show()
    sys.exit(app.exec())


if __name__ == '__main__':
    main()




main.ui file:

<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>1300</width>
    <height>700</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Prism</string>
  </property>
  <layout class="QVBoxLayout" name="verticalLayout_3">

    <item>
     <widget class="QTextEdit" name="textEdit">
      <property name="font">
       <font>
        <pointsize>11</pointsize>
       </font>
      </property>
      <property name="readOnly">
       <bool>true</bool>
      </property>
     </widget>
    </item>

   <item>
    <widget class="QStackedWidget" name="stackedWidget">
     <widget class="QWidget" name="page">
      <layout class="QVBoxLayout" name="verticalLayout_2">
       <item>
        <layout class="QHBoxLayout" name="horizontalLayout" stretch="0,0,3">
         <item>
          <layout class="QVBoxLayout" name="verticalLayout" stretch="0,0,2">
           <item>
            <widget class="QLabel" name="label">
             <property name="text">
              <string>Interfaces</string>
             </property>
            </widget>
           </item>
           <item>
            <widget class="QListWidget" name="listWidget"/>
           </item>
           <item>
            <widget class="QListWidget" name="listWidget_2"/>
           </item>
          </layout>
         </item>
         <item>
          <widget class="QListWidget" name="listWidget_3"/>
         </item>
         <item>
          <widget class="QScrollArea" name="scrollArea">
           <property name="widgetResizable">
            <bool>true</bool>
           </property>
           <widget class="QWidget" name="scrollAreaWidgetContents">
            <property name="geometry">
             <rect>
              <x>0</x>
              <y>0</y>
              <width>50</width>
              <height>40</height>
             </rect>
            </property>
            <layout class="QVBoxLayout" name="verticalLayout_4">
             <item>
              <layout class="QGridLayout" name="gridLayout"/>
             </item>
            </layout>
           </widget>
          </widget>
         </item>
        </layout>
       </item>
      </layout>
     </widget>
     <widget class="QWidget" name="page_2"/>
    </widget>
   </item>
   <item>
    <widget class="QTableWidget" name="tableWidget">
     <column>
      <property name="text">
       <string>One</string>
      </property>
     </column>
     <column>
      <property name="text">
       <string>Two</string>
      </property>
     </column>
     <column>
      <property name="text">
       <string>Three</string>
      </property>
     </column>
    </widget>
   </item>
  </layout>
  <zorder>tableWidget</zorder>
  <zorder>stackedWidget</zorder>
 </widget>
 <resources/>
 <connections/>
</ui>



ai.py file:

#This file has lots of codes to display data from psutil
#I am printing only a simple string for testing

def main():
    pf('Hello', end='')

def pf(string, **kwargs):
    if 'flush' in kwargs:
        print(string, **kwargs)
    else:
        print(string, flush = True, **kwargs)

if __name__ == '__main__':
    main()


What I have tried:

The program works fine if the main.ui is structured this way (below) with only one widget within the QMainWindow class. But the program won't work with the main.ui file above (the one with multiple widgets).

# Good main.ui code

<?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>800</width>
    <height>600</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>MainWindow</string>
  </property>
  <widget class="QWidget" name="centralwidget">
   <layout class="QVBoxLayout" name="verticalLayout">
    <item>
     <widget class="QTextEdit" name="textEdit">
      <property name="font">
       <font>
        <pointsize>10</pointsize>
       </font>
      </property>
      <property name="readOnly">
       <bool>true</bool>
      </property>
     </widget>
    </item>
   </layout>
  </widget>
  <widget class="QStatusBar" name="statusbar"/>
 </widget>
 <resources/>
 <connections/>
</ui>


Any help will be greatly appreciated!
Posted
Updated 24-Dec-20 2:22am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900