0

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_())


A-Aron
  • 29
  • 6
  • From what I understand you, you want the prints to be saved in the .log file and that information is also displayed in the QTextEdit, am I correct? – eyllanesc Jan 20 '20 at 18:11
  • Not exactly. I don't have much care of a log file but it would be a nice to have. What I want is a console like textbox that all print functions output to, which updates as soon as a print function is executed. – A-Aron Jan 20 '20 at 18:15
  • I don't understand you, explain yourself better, then why don't you add the text directly QTextEdit instead of using "print", for example change `print('None are selected.')` to `self.te.append('None are selected.')` – eyllanesc Jan 20 '20 at 18:17
  • How would this work if I'm importing a module that has a print function? The problem right now is the QTextEdit is only updated after the entire function is completed. I have a function that takes about 5 minutes to run, and it prints to the console at each stage. How would I show the updates to the console inside the QTextEdit immediately? – A-Aron Jan 20 '20 at 18:38

0 Answers0