0

I have a simple pyqt5 application writing numbers inside a textBrowser Widget. But when the focus of the pyqt5 window is lost in other words when I click outside the window during the process it stops writing to the textBrowser until the method finishes. How can I fix that?

Code:

import sys
import time
from PyQt5 import QtWidgets
from PyQt5.QtWidgets import QDialog, QApplication
from PyQt5.uic import loadUi
from logger import Logger


class MainWindow(QDialog):
    logger = None
    def __init__(self, logger: Logger = None):
        super(MainWindow, self).__init__()
        loadUi('main.ui', self)
        self.widget = QtWidgets.QStackedWidget()
        self.widget.setFixedWidth(600)
        self.widget.setFixedHeight(400)
        self.widget.addWidget(self)
        self.logger = logger
        self.widget.show()
        self.start_task_button.clicked.connect(self.start_task)

    def start_task(self):
        numbers = [1,2,3,4,5,6]
        self.logger.log("start")
        for i in numbers:
            time.sleep(3)
            self.logger.log(i)

    def log(self, message):
        self.textBrowser.append(message)
        self.textBrowser.repaint()
        print(message)

# init ui
app = QApplication(sys.argv)
logger = Logger()
main_window = MainWindow(logger)
logger.register(main_window)
main_window.show()
app.exec_()

UI: pastebin of the loaded ui file: https://pastebin.com/h8Bs5r5K

Image: As you can see the console shows already 5 while the textBrowser only shows 1 (the moment I clicked outside the window) it should be the same.

As you can see the console shows already 5 while the textBrowser only shows 1 (the moment I clicked outside the window) it should be the same

nanobot
  • 108
  • 5
  • 18
  • Don't use time.sleep as it blocks the GUI, check https://stackoverflow.com/questions/41545300/equivalent-to-time-sleep – eyllanesc Nov 24 '20 at 23:34
  • Ahh ok my promblem then is that I'm using a library pyautogui which has functions that also stop the GUI. So do I have to runn my method in another thread to keep the gui responsive? – nanobot Nov 24 '20 at 23:51
  • 1
    I do not see anything from pyautogui so I will not comment on that, although one option is to use threading but you must know the limitations, so each case must be analyzed since many times there are better solutions. for example in your case just use a QTimer – eyllanesc Nov 24 '20 at 23:58

0 Answers0