I'm Beginner in Python. I was learning Pyqt5. But I could not find any video on youtube about QThread. and I don't have any friends who know PyQt5. So I need help from you. The problem is, I can't run another service while the mainwindow is running. If I do, then the mainwindow freeze and the background service runs. I faced this same problem in tkinter, but python's threading module saved me. but in pyqt5, Threading module doesn't work at all. Most of the cases it gives the following error: QObject: Cannot create children for a parent that is in a different thread. So I'm trying to use QThread. but another problem is that I can't change anything from the mainwindow when I use QThread. Example: I have a window and there is a label and a button. Now When I press the Button it should wait 10 sec and in every seconds, the label's text should be changed like: 1 sec passed, 2 sec passed. But It doesn't shows up. GUI freeze for 10 sec and after ending the background task, It shows 10 sec passed.
This simple Programme is an example. I'm doing a little big project (as a beginner), where my gui freezes. I know you need code to help me.....
Take it:
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from time import sleep
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(260, 200)
MainWindow.setMinimumSize(QtCore.QSize(260, 200))
MainWindow.setMaximumSize(QtCore.QSize(260, 200))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
# Here is my Label________________________
self.txt = QtWidgets.QLabel(self.centralwidget)
self.txt.setGeometry(QtCore.QRect(1, 29, 261, 48))
self.txt.setObjectName("txt")
# & here is my button______________________________
self.btn = QtWidgets.QPushButton(self.centralwidget)
self.btn.setGeometry(QtCore.QRect(90, 120, 75, 24))
font = QtGui.QFont()
font.setPointSize(10)
self.btn.setFont(font)
self.btn.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.btn.setObjectName("btn")
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.function()
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.txt.setText(_translate("MainWindow", "<html><head/><body><p align=\"center\"><span style=\" font-size:15px;\">Press the button to set 10 sec timer</span></p></body></html>"))
self.btn.setText(_translate("MainWindow", "Set Now"))
def function(self):
self.btn.clicked.connect(self.setit)
def setit(self):
lb=" sec passed"
for i in range(10):
sleep(1)
self.txt.setText(self.msg(str(i+1)+lb))
def msg(self,txt):
text="<html><head/><body><p align=\"center\"><span style=\" font-size:15px;\">{}</span></p></body></html>".format(txt)
return text
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
I want to run the `self.setit` function in thread. But in QThread I can't Edit the `QLabel`. That's the problem. Help Me! pls