0

I've been trying out python GUI frameworks for a multithreaded project that requires the GUI to run entirely on a thread started from the main thread with a reference to a variable being updated on a different thread. However, when I run the GUI on a thread other than the main one it instantly closes and I get

"QApplication::exec: Must be called from the main thread"

when using pyqt5 and

"RuntimeError: main thread is not in main loop"

when using tkinter.

I was wondering if anyone knew another python GUI framework that would let me run it on a thread other than the main thread or if there is a way to work around this errors.

Here is an example replicating the first message with pyqt5

import  threading, sys

from PyQt5.QtCore import QObject
from PyQt5.QtWidgets import QMainWindow, QApplication


class Example(QMainWindow):

    def __init__(self, app):
        super().__init__()

        self.app=app
        self.initUI()


    def initUI(self):
        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Example')
        self.show()

    def principal(self):

        sys.exit(self.app.exec_())


def main():

    app = QApplication(sys.argv)
    example = Example(app)

    #sys.exit(example.app.exec_())

    exampleThread = threading.Thread(target=example.principal, name='example')
    exampleThread.start()

main()
eyllanesc
  • 235,170
  • 19
  • 170
  • 241
  • Every GUI library handles many internal elements that are not thread-safe. Why are they not thread-safe? because these elements are modified by several parts of the GUI and if they made thread - safe through mutex, traffic lights, etc. they would cause the transmission of information to suffer a large bottleneck. So by design no GUI is thread-safe. – eyllanesc Jan 30 '20 at 20:59
  • Thanks for the quick response! I'll start implementing this another way keeping in mind what you said. – German Martinez Jan 30 '20 at 21:24

0 Answers0