0

I have been googling for hours on “PyQt5 update text on screen” but I am completely stuck because I can’t understand the logic of PyQt5.

  • All I want is a simple window with text on it that I can update.
  • I am not interested in diving deep into all of the different functions that PyQt5 has.

I have a program which displays a window with a text on the screen using the following:

def __init__(self) -> None:
    self.app = QApplication(sys.argv)  
    self.main_window = MainWindow()
    self.main_window.move(2350, 360)
    self.textBox = QtWidgets.QLabel(self.main_window)
    
def RunWindow(self):
    self.textBox.setText("input")
    self.main_window.show()
    self.app.exec_()

The problem is that I can’t update the text because of this:
(1) No window will appear until the syntax “app.exec_()” is run.
(2) When the line “app.exec_()” runs, it blocks any other code that follows it.

This means that when i try to run a function which updates the text, the code never reaches the function, and I am stuck with the same text as when the program started.

gelbrekt
  • 179
  • 1
  • 2
  • 10
  • 1
    You're not expected to have code "after" `app.exec()` if that code is related to user interaction or changes in the UI. You must properly use signals and slots instead, and connect them according to the requirements of your program. Most signals happen after the user does something, others react to the system or some specific aspect of the object used; for instance, if you need to call a function periodically and/or after a specified interval, create a [QTimer](//doc.qt.io/qt-5/qtimer.html) and connect its `timeout` signal to the function. – musicamante Dec 30 '21 at 23:31
  • 1
    Read more about [signal and slots](//wiki.qt.io/Qt_for_Python_Signals_and_Slots), and have a look at some tutorials: https://wiki.python.org/moin/PyQt/Tutorials https://wiki.qt.io/Qt_for_Python/Tutorial (I strongly suggest to avoid youtube tutorials as your first source of documentation and experience). – musicamante Dec 30 '21 at 23:33
  • 1
    See: [How to implement a simple button in PyQt](https://stackoverflow.com/q/8762870/984421). – ekhumoro Dec 31 '21 at 13:43

0 Answers0