Unsolved PyQt not responding with example code
-
Hello i made simple code like this. While my function is counting my program is not responding.
Please can somebody show me how to change this code to get it work with that "Threads" thing ?
THANK YOU !
import sys from PyQt5 import QtWidgets from PyQt5.QtWidgets import * class Window(QMainWindow): def __init__(self): super().__init__() self.setFixedSize(300, 300) self.move(100,100) self.setWindowTitle("Hello") self.label = QLabel(self) self.label.setText("Not done") self.label.resize(400,500) self.label.move(0,5) self.b1 = QtWidgets.QPushButton(self) self.b1.setText("Count") self.b1.move(100, 120) self.b1.resize(130, 32) self.b1.clicked.connect(self.Count) self.show() def Count(self): self.i = 0 while self.i != 50000000: self.i +=1 self.label.setText("DONE") if __name__ == "__main__": app = QApplication(sys.argv) window = Window() sys.exit(app.exec())
-
Hi,
Because your counting loop is blocking Qt's event loop.
-
And how can i improve this code to not doing it ? Can you tell me what to add to this code ? Please.
-
If you want to run long lasting tasks without blocking your GUI either use QtConcurrent or move it to another thread.
-
I just can not find on internet how to properly use this. Where to put this command or how to work with it.
-
What command are you talking about ?
-
I mean this QtConcurrent. I dont know how to use it, where to put this thing in this code.
-
Did you read the documentation I linked ?
-
@Samuel-Bachorik This tutorial about multithreading may help you solve your problem.
-
@unplug Thank you man !!!!!
-
Hi Sir,
In threading is it possible to run with out sleep ? -
@Amarnath-Reddy-S said in PyQt not responding with example code:
In threading is it possible to run with out sleep ?
Yes, don't call sleep() then.
Keep in mind that the thread will consume 100% of one CPU core.