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 QMainWindow with only one widget in it.
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)
# self.output.ensureCursorVisible()
def stdoutReady(self):
text = str(self.process.readAllStandardOutput())
print(text.strip())
self.append('
' + text)
def stderrReady(self):
text = str(self.process.readAllStandardError())
# print (text.strip())
self.append(text)
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()
The program works fine if the main.ui is structured this way (below) with only one widget within the QMainWindow class.
# 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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…