I am trying to build a GUI that has a console that will show the live console output.
At the moment I am able to output the print commands to a log file, then read the log file and output this to QTextEdit.
However, this only updates after the process is run. How do I make it so that it updates live?
My current code:
import sys
from PyQt5 import QtGui
from PyQt5.QtCore import Qt, QDate
from PyQt5.QtWidgets import (
QApplication,
QCheckBox,
QGridLayout,
QGroupBox,
QMenu,
QPushButton,
QRadioButton,
QVBoxLayout,
QWidget,
QFrame,
QDateEdit,
QTextEdit,
QPlainTextEdit,
QMessageBox)
# test imports
class Window(QWidget):
def __init__(self, parent=None):
super(Window, self).__init__(parent)
self.setWindowIcon(QtGui.QIcon('logo.png'))
self.setWindowTitle("Test")
self.resize(800, 500)
grid = QGridLayout()
grid.addWidget(self.group1(), 0, 0)
grid.addWidget(self.group2(), 0, 1)
grid.addWidget(self.group2(), 0, 2)
grid.addWidget(self.console_layout(), 1, 0, 1, 3)
self.setLayout(grid)
def group1(self):
groupBox = QGroupBox("Box 1")
groupBox.setMaximumHeight = 100
grp1_date = QDate.currentDate().addDays(-1)
self.grp1_dateSelect = QDateEdit()
self.grp1_dateSelect.setDate(grp1_date)
# define checkboxes
self.grp1_checkbox1 = QCheckBox("Task 1")
self.grp1_checkbox2 = QCheckBox("Task 2")
self.grp1_checkbox3 = QCheckBox("Task 3")
# define buttons
self.grp1_button1 = QPushButton('Run')
self.grp1_button2 = QPushButton('Run')
# define button dimensions and actions
self.grp1_button1.setMaximumWidth(75)
self.grp1_button2.setMaximumWidth(75)
self.grp1_button1.clicked.connect(self.grp1_btn1_onClick)
# self.grp1_button2.clicked.connect(self.grp1_btn2_onClick)
# define separator lines
self.grp1_separatorLine = QFrame(frameShape=QFrame.HLine, frameShadow=QFrame.Sunken)
vbox = QVBoxLayout()
vbox.addWidget(self.grp1_dateSelect)
vbox.addWidget(self.grp1_checkbox1)
vbox.addWidget(self.grp1_checkbox2)
vbox.addWidget(self.grp1_button1)
vbox.addWidget(self.grp1_separatorLine)
vbox.addWidget(self.grp1_checkbox3)
vbox.addWidget(self.grp1_button2)
vbox.addStretch(1)
groupBox.setLayout(vbox)
return groupBox
def group2(self):
groupBox = QGroupBox("Box 1")
return groupBox
def grp1_btn1_onClick(self):
# create the log file
old_stdout = sys.stdout
log_file = open("message.log","w")
sys.stdout = log_file
date = self.grp1_dateSelect.date().toString("yyyy-MM-dd")
if self.grp1_checkbox1.isChecked() == False and self.grp1_checkbox2.isChecked() == False:
print('None are selected.')
reply = QMessageBox.warning(self, 'Warning', 'You must select at least 1 option.', QMessageBox.Ok, QMessageBox.Ok)
elif self.grp1_checkbox1.isChecked() and self.grp1_checkbox2.isChecked():
print('Both are selected!')
elif self.grp1_checkbox1.isChecked():
print('Task 1 is selected.')
hello()
elif self.grp1_checkbox2.isChecked():
print('Task 2 is selected.')
sys.stdout = old_stdout
log_file.close()
# read the log file and output to QTextEdit
with open('message.log', 'r') as f:
text = f.read()
cursor = self.te.textCursor()
self.te.setText(text)
self.te.setTextCursor(cursor)
def btn2_onClick(self):
date = self.grp2_dateSelect.date().toString("yyyy-MM-dd")
print(date)
def console_layout(self):
groupBox = QGroupBox("Console")
self.te = QTextEdit()
vbox = QVBoxLayout()
vbox.addWidget(self.te)
groupBox.setLayout(vbox)
return groupBox
def hello():
print('hello world!')
if __name__ == '__main__':
app = QApplication(sys.argv)
window = Window()
window.show()
sys.exit(app.exec_())