How to update GUI from a thread?
I'm trying to update the QLabel in the GUI with the number, generated in a thread. Can this be done?
Code:
import threading, random, sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5 import QtCore, QtTest
def update():
while True:
n = random.randint(0,22)
QtTest.QTest.qWait(500)
threading.Thread(target=update, daemon = True).start()
def __init__(self, *args, **kwargs):
super(MainWindow, self).__init__(*args, **kwargs)
self.setFixedSize(200, 200)
self.labl.setText("value of n goes here")
window = MainWindow()
window.show()
sys.exit(app.exec_())
Re: How to update GUI from a thread?
The usual way is to have your thread emit a signal that is handled by a slot in the your GUI thread, where you pass the value as an argument in the signal.
This also means you probably need to use QThread for your thread implementation.
Re: How to update GUI from a thread?