waitForDone() I am not compatible with gui , gui is frozen
-
"Hello everyone!
I am currently working on a multithreading program using QtCore.QThreadPool() and QtCore.QRunnable(). I want to make sure that each time a thread A is created and executed, it must be completed before creating and running thread B. I use waitForDone() to ensure that thread B is not created until thread A is finished, but when using waitForDone(), my program freezes the GUI and it cannot be interacted with until thread A is completed. Is there a way to keep the GUI responsive while thread A is running?
-
"Hello everyone!
I am currently working on a multithreading program using QtCore.QThreadPool() and QtCore.QRunnable(). I want to make sure that each time a thread A is created and executed, it must be completed before creating and running thread B. I use waitForDone() to ensure that thread B is not created until thread A is finished, but when using waitForDone(), my program freezes the GUI and it cannot be interacted with until thread A is completed. Is there a way to keep the GUI responsive while thread A is running?
@khong-muon-them-nhieu-sai-lam
If you call a blocking function likewaitForDone()
from the GUI thread it will naturally block that thread, and the UI will be unresponsive. Instead just put a slot on the signal for thread A finishing and have that start thread B, that's the Qt way. -
@khong-muon-them-nhieu-sai-lam
If you call a blocking function likewaitForDone()
from the GUI thread it will naturally block that thread, and the UI will be unresponsive. Instead just put a slot on the signal for thread A finishing and have that start thread B, that's the Qt way.@JonB
Can you give me an example? I'm just a newbie and don't understand much about what you're saying !this is my code :
this is my code
Form implementation generated from reading ui file 'untitled.ui'
Created by: PyQt6 UI code generator 6.4.0
WARNING: Any manual changes made to this file will be lost when pyuic6 is
run again. Do not edit this file unless you know what you are doing.
import mofbchange
import sys , time
from PyQt5 import QtCore, QtGui, QtWidgetsdef mo_file():
lines = [line.strip() for line in open("file.txt")]
non_empty_lines = list(filter(lambda x: x, lines))
return non_empty_lines, len(non_empty_lines)class RunnerSignals(QtCore.QObject):
progressed = QtCore.pyqtSignal(int)
messaged = QtCore.pyqtSignal(str)class Ui_MainWindow(object):
def setupUi(self, MainWindow, a, b,list):
self.__pool = QtCore.QThreadPool()
self.is_running = True
MainWindow.setObjectName("MainWindow")
MainWindow.resize(848, 595)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.batdau = QtWidgets.QPushButton(self.centralwidget)
self.batdau.setGeometry(QtCore.QRect(660, 10, 131, 81))
self.batdau.setCursor(QtGui.QCursor(QtCore.Qt.CursorShape.ArrowCursor))
self.batdau.setAutoDefault(False)
self.batdau.setObjectName("batdau")
self.ketthuc = QtWidgets.QToolButton(self.centralwidget)
self.ketthuc.setGeometry(QtCore.QRect(660, 100, 131, 71))
self.ketthuc.setObjectName("ketthuc")
self.layacc = QtWidgets.QPushButton(self.centralwidget)
self.layacc.setGeometry(QtCore.QRect(660, 180, 131, 81))
self.layacc.setObjectName("layacc")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setGeometry(QtCore.QRect(650, 320, 177, 20))
self.splitter.setOrientation(QtCore.Qt.Orientation.Horizontal)
self.splitter.setObjectName("splitter")
self.label = QtWidgets.QLabel(self.splitter)
self.label.setObjectName("label")
self.luong = QtWidgets.QLineEdit(self.splitter)
self.luong.setObjectName("luong")# tao bang self.bang = QtWidgets.QTableWidget(self.centralwidget) self.bang.setGeometry(QtCore.QRect(10, 10, 631, 541)) self.bang.setObjectName("bang") self.bang.setColumnCount(b) self.bang.setRowCount(a) self.bang.setColumnWidth(0, 220) self.bang.setColumnWidth(1, 375) #tao checkbox for row in range(len(list)): checkbox = QtWidgets.QCheckBox() checkbox.setChecked(True) self.item = QtWidgets.QTableWidgetItem(str(list[row])) self.item.setCheckState(QtCore.Qt.CheckState.Checked) self.bang.setItem(row, 0, self.item) # menu nay no MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 848, 21)) self.menubar.setObjectName("menubar") self.menuChange_acc = QtWidgets.QMenu(self.menubar) self.menuChange_acc.setObjectName("menuChange_acc") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.menubar.addAction(self.menuChange_acc.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.batdau.setText(_translate("MainWindow", "Bắt đầu")) self.ketthuc.setText(_translate("MainWindow", "Tạm Dừng")) self.layacc.setText(_translate("MainWindow", "Lấy ra acc chưa chạy ")) self.label.setText(_translate("MainWindow", "Điền Luồng đầu tiên :")) __sortingEnabled = self.bang.isSortingEnabled() self.bang.setSortingEnabled(False) self.bang.setSortingEnabled(__sortingEnabled) self.menuChange_acc.setTitle(_translate("MainWindow", "Change acc")) # tạo tên cho cột và hàng column_labels = ['Tài khoản', 'Hoạt Động '] self.bang.setHorizontalHeaderLabels(column_labels) # cai luong mac dinh self.luong.setText("1") # self.batdau.clicked.connect(lambda: self.lay_list_chay(listacc)) self.ketthuc.clicked.connect(self.tam_dung) self.batdau.clicked.connect(self.change_info) def layluong(self): self.luongchay = self.luong.text() self.luongchay1 = int(self.luongchay) return self.luongchay1 def lay_list_checkstatic(self): self.listacc = [] for i in range(self.bang.rowCount()): item = self.bang.item(i, 0) # 0 is the column index of the checkbox column if item.checkState() == QtCore.Qt.CheckState.Checked: self.listacc.append(item) return self.listacc def tam_dung(self): if self.is_running: self.is_running = False self.ketthuc.setText("Tiếp Tục") print(self.is_running) else: self.is_running = True self.ketthuc.setText("Tạm Dừng") print(self.is_running) self.change_info() def change_info(self): if self.is_running == False: self.is_running = True self.ketthuc.setText("Tạm Dừng") self.chieudaicuaacc = len(self.lay_list_checkstatic()) self.soluong = self.layluong() if self.chieudaicuaacc % self.soluong != 0 : self.lanchay = int((self.chieudaicuaacc / self.soluong ))+1 else: self.lanchay = int(self.chieudaicuaacc / self.soluong ) if self.is_running == True: max_rows = self.soluong # Số lượng hàng tối đa cần lấy ra for a in range(self.lanchay): self.acc = self.lay_list_checkstatic() for b in range(self.soluong): try: print("jojojo") self.acc[b].setCheckState(QtCore.Qt.CheckState.Unchecked) except: pass self.worker_1 = Runner(self.acc[:max_rows]) self.__pool.start(self.worker_1) self.__pool.waitForDone() #worker_1.signals.progressed.connect(receiver=,) if self.is_running == False: self.ketthuc.setText("Tiếp Tục")
class Runner(QtCore.QRunnable):
def init(self,a):
super(Runner, self).init()
self.a = a
self.signals = RunnerSignals()def run(self): mofbchange.mochom()
if name == "main":
app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() listacc, sohang = mo_file() ui.setupUi(MainWindow, sohang, 2,listacc) MainWindow.show() sys.exit(app.exec())
-
@JonB
Can you give me an example? I'm just a newbie and don't understand much about what you're saying !this is my code :
this is my code
Form implementation generated from reading ui file 'untitled.ui'
Created by: PyQt6 UI code generator 6.4.0
WARNING: Any manual changes made to this file will be lost when pyuic6 is
run again. Do not edit this file unless you know what you are doing.
import mofbchange
import sys , time
from PyQt5 import QtCore, QtGui, QtWidgetsdef mo_file():
lines = [line.strip() for line in open("file.txt")]
non_empty_lines = list(filter(lambda x: x, lines))
return non_empty_lines, len(non_empty_lines)class RunnerSignals(QtCore.QObject):
progressed = QtCore.pyqtSignal(int)
messaged = QtCore.pyqtSignal(str)class Ui_MainWindow(object):
def setupUi(self, MainWindow, a, b,list):
self.__pool = QtCore.QThreadPool()
self.is_running = True
MainWindow.setObjectName("MainWindow")
MainWindow.resize(848, 595)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.batdau = QtWidgets.QPushButton(self.centralwidget)
self.batdau.setGeometry(QtCore.QRect(660, 10, 131, 81))
self.batdau.setCursor(QtGui.QCursor(QtCore.Qt.CursorShape.ArrowCursor))
self.batdau.setAutoDefault(False)
self.batdau.setObjectName("batdau")
self.ketthuc = QtWidgets.QToolButton(self.centralwidget)
self.ketthuc.setGeometry(QtCore.QRect(660, 100, 131, 71))
self.ketthuc.setObjectName("ketthuc")
self.layacc = QtWidgets.QPushButton(self.centralwidget)
self.layacc.setGeometry(QtCore.QRect(660, 180, 131, 81))
self.layacc.setObjectName("layacc")
self.splitter = QtWidgets.QSplitter(self.centralwidget)
self.splitter.setGeometry(QtCore.QRect(650, 320, 177, 20))
self.splitter.setOrientation(QtCore.Qt.Orientation.Horizontal)
self.splitter.setObjectName("splitter")
self.label = QtWidgets.QLabel(self.splitter)
self.label.setObjectName("label")
self.luong = QtWidgets.QLineEdit(self.splitter)
self.luong.setObjectName("luong")# tao bang self.bang = QtWidgets.QTableWidget(self.centralwidget) self.bang.setGeometry(QtCore.QRect(10, 10, 631, 541)) self.bang.setObjectName("bang") self.bang.setColumnCount(b) self.bang.setRowCount(a) self.bang.setColumnWidth(0, 220) self.bang.setColumnWidth(1, 375) #tao checkbox for row in range(len(list)): checkbox = QtWidgets.QCheckBox() checkbox.setChecked(True) self.item = QtWidgets.QTableWidgetItem(str(list[row])) self.item.setCheckState(QtCore.Qt.CheckState.Checked) self.bang.setItem(row, 0, self.item) # menu nay no MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 848, 21)) self.menubar.setObjectName("menubar") self.menuChange_acc = QtWidgets.QMenu(self.menubar) self.menuChange_acc.setObjectName("menuChange_acc") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.menubar.addAction(self.menuChange_acc.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.batdau.setText(_translate("MainWindow", "Bắt đầu")) self.ketthuc.setText(_translate("MainWindow", "Tạm Dừng")) self.layacc.setText(_translate("MainWindow", "Lấy ra acc chưa chạy ")) self.label.setText(_translate("MainWindow", "Điền Luồng đầu tiên :")) __sortingEnabled = self.bang.isSortingEnabled() self.bang.setSortingEnabled(False) self.bang.setSortingEnabled(__sortingEnabled) self.menuChange_acc.setTitle(_translate("MainWindow", "Change acc")) # tạo tên cho cột và hàng column_labels = ['Tài khoản', 'Hoạt Động '] self.bang.setHorizontalHeaderLabels(column_labels) # cai luong mac dinh self.luong.setText("1") # self.batdau.clicked.connect(lambda: self.lay_list_chay(listacc)) self.ketthuc.clicked.connect(self.tam_dung) self.batdau.clicked.connect(self.change_info) def layluong(self): self.luongchay = self.luong.text() self.luongchay1 = int(self.luongchay) return self.luongchay1 def lay_list_checkstatic(self): self.listacc = [] for i in range(self.bang.rowCount()): item = self.bang.item(i, 0) # 0 is the column index of the checkbox column if item.checkState() == QtCore.Qt.CheckState.Checked: self.listacc.append(item) return self.listacc def tam_dung(self): if self.is_running: self.is_running = False self.ketthuc.setText("Tiếp Tục") print(self.is_running) else: self.is_running = True self.ketthuc.setText("Tạm Dừng") print(self.is_running) self.change_info() def change_info(self): if self.is_running == False: self.is_running = True self.ketthuc.setText("Tạm Dừng") self.chieudaicuaacc = len(self.lay_list_checkstatic()) self.soluong = self.layluong() if self.chieudaicuaacc % self.soluong != 0 : self.lanchay = int((self.chieudaicuaacc / self.soluong ))+1 else: self.lanchay = int(self.chieudaicuaacc / self.soluong ) if self.is_running == True: max_rows = self.soluong # Số lượng hàng tối đa cần lấy ra for a in range(self.lanchay): self.acc = self.lay_list_checkstatic() for b in range(self.soluong): try: print("jojojo") self.acc[b].setCheckState(QtCore.Qt.CheckState.Unchecked) except: pass self.worker_1 = Runner(self.acc[:max_rows]) self.__pool.start(self.worker_1) self.__pool.waitForDone() #worker_1.signals.progressed.connect(receiver=,) if self.is_running == False: self.ketthuc.setText("Tiếp Tục")
class Runner(QtCore.QRunnable):
def init(self,a):
super(Runner, self).init()
self.a = a
self.signals = RunnerSignals()def run(self): mofbchange.mochom()
if name == "main":
app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() listacc, sohang = mo_file() ui.setupUi(MainWindow, sohang, 2,listacc) MainWindow.show() sys.exit(app.exec())
@khong-muon-them-nhieu-sai-lam
Firstly if you want to post blocks of code like this please use the forum's Code tags to surround it, especially for Python where indentation is vital.Don't call
self.__pool.waitForDone()
. Instead something like:self.worker1.signals.finished.connect(self.someMethodWhichStartsTheNextThread) # or you can use a Python *lambda* here
You will want to look through the documentation of whatever
QThreadPool
orQRunnable
you are using. You need to find a signal for the thread being finished (which is whatwaitForDone()
will do internally, but it blocks). -
@khong-muon-them-nhieu-sai-lam
Firstly if you want to post blocks of code like this please use the forum's Code tags to surround it, especially for Python where indentation is vital.Don't call
self.__pool.waitForDone()
. Instead something like:self.worker1.signals.finished.connect(self.someMethodWhichStartsTheNextThread) # or you can use a Python *lambda* here
You will want to look through the documentation of whatever
QThreadPool
orQRunnable
you are using. You need to find a signal for the thread being finished (which is whatwaitForDone()
will do internally, but it blocks).@JonB
I have tried the solution you provided but it did not solve my problem. Let me briefly explain how my program works. I have a number of rows and I want to split them into several parts, each with a certain number of rows. For example, if I have 10 rows, I will split them into 5 parts, each with 2 rows. For each part, I will create a corresponding number of threads to process the text of each row. After processing the first 2 rows of part 1, it will then process the next 2 rows and so on. I have tried using QThreadPool, QRunnable, QThread but they all freeze the GUI ,it doesn't freeze the gui if i don't use waitfordone in qthreadpool or wait function in qthread . I cannot use your suggested method because it still freezes the GUI. -
@JonB
I have tried the solution you provided but it did not solve my problem. Let me briefly explain how my program works. I have a number of rows and I want to split them into several parts, each with a certain number of rows. For example, if I have 10 rows, I will split them into 5 parts, each with 2 rows. For each part, I will create a corresponding number of threads to process the text of each row. After processing the first 2 rows of part 1, it will then process the next 2 rows and so on. I have tried using QThreadPool, QRunnable, QThread but they all freeze the GUI ,it doesn't freeze the gui if i don't use waitfordone in qthreadpool or wait function in qthread . I cannot use your suggested method because it still freezes the GUI.@khong-muon-them-nhieu-sai-lam said in waitForDone() I am not compatible with gui , gui is frozen:
I have tried using QThreadPool, QRunnable, QThread but they all freeze the GUI. I cannot use your suggested method because it still freezes the GUI.
What freezes the GUI? Using some kind of
waitFor...()
will do that. But assuming you do not what do you think QThreadPool/QRunnable/QThread do to freeze the UI? They don't. So maybe your code is wrong.Don't forget as a totally separate matter if you call Python code in your threads you have to take account of Python GIL limitations on multithreading.
-
@khong-muon-them-nhieu-sai-lam said in waitForDone() I am not compatible with gui , gui is frozen:
I have tried using QThreadPool, QRunnable, QThread but they all freeze the GUI. I cannot use your suggested method because it still freezes the GUI.
What freezes the GUI? Using some kind of
waitFor...()
will do that. But assuming you do not what do you think QThreadPool/QRunnable/QThread do to freeze the UI? They don't. So maybe your code is wrong.Don't forget as a totally separate matter if you call Python code in your threads you have to take account of Python GIL limitations on multithreading.
@JonB
true when i don't use waitfordone or wait the gui doesn't freeze. i asked chatgpt and it told me that since i use waitfordone thread which coincides with gui's thread it will freeze the gui . but I don't know how to handle these 2 streams separately, I've been tired of this problem for a week and still haven't solved it @@ -
@JonB
true when i don't use waitfordone or wait the gui doesn't freeze. i asked chatgpt and it told me that since i use waitfordone thread which coincides with gui's thread it will freeze the gui . but I don't know how to handle these 2 streams separately, I've been tired of this problem for a week and still haven't solved it @@@khong-muon-them-nhieu-sai-lam
I explained earlier. Told you not to usewaitFor...()
and to use signals instead. "I cannot use your suggested method because it still freezes the GUI." but now you admit you have putwaitFor...()
in. Suggested the code you will need. Up to you. -
@khong-muon-them-nhieu-sai-lam
I explained earlier. Told you not to usewaitFor...()
and to use signals instead. "I cannot use your suggested method because it still freezes the GUI." but now you admit you have putwaitFor...()
in. Suggested the code you will need. Up to you.@JonB
i used your method but it can't solve my problem
i have listened to you .but still not working. or i wrote the wrong code . I hope there is another workaround to make the gui not freeze. Thank you very much for helping me. -
@JonB
i used your method but it can't solve my problem
i have listened to you .but still not working. or i wrote the wrong code . I hope there is another workaround to make the gui not freeze. Thank you very much for helping me.@khong-muon-them-nhieu-sai-lam No need for any workaround. Simply use Qt properly: use signals and slots just like @JonB suggested. If your current code does not work then please post it.
-
@khong-muon-them-nhieu-sai-lam No need for any workaround. Simply use Qt properly: use signals and slots just like @JonB suggested. If your current code does not work then please post it.
my code : import mofbchange import sys , time from PyQt5 import QtCore, QtGui, QtWidgets def mo_file(): lines = [line.strip() for line in open("file.txt")] non_empty_lines = list(filter(lambda x: x, lines)) return non_empty_lines, len(non_empty_lines) class RunnerSignals(QtCore.QObject): progressed = QtCore.pyqtSignal(int) finish = QtCore.pyqtSignal(str) class Ui_MainWindow(object): def setupUi(self, MainWindow, a, b,list): self.is_running = True pool = QtCore.QThreadPool.globalInstance() MainWindow.setObjectName("MainWindow") MainWindow.resize(848, 595) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.batdau = QtWidgets.QPushButton(self.centralwidget) self.batdau.setGeometry(QtCore.QRect(660, 10, 131, 81)) self.batdau.setCursor(QtGui.QCursor(QtCore.Qt.CursorShape.ArrowCursor)) self.batdau.setAutoDefault(False) self.batdau.setObjectName("batdau") self.ketthuc = QtWidgets.QToolButton(self.centralwidget) self.ketthuc.setGeometry(QtCore.QRect(660, 100, 131, 71)) self.ketthuc.setObjectName("ketthuc") self.layacc = QtWidgets.QPushButton(self.centralwidget) self.layacc.setGeometry(QtCore.QRect(660, 180, 131, 81)) self.layacc.setObjectName("layacc") self.splitter = QtWidgets.QSplitter(self.centralwidget) self.splitter.setGeometry(QtCore.QRect(650, 320, 177, 20)) self.splitter.setOrientation(QtCore.Qt.Orientation.Horizontal) self.splitter.setObjectName("splitter") self.label = QtWidgets.QLabel(self.splitter) self.label.setObjectName("label") self.luong = QtWidgets.QLineEdit(self.splitter) self.luong.setObjectName("luong") self.tho = {} # tao bang self.bang = QtWidgets.QTableWidget(self.centralwidget) self.bang.setGeometry(QtCore.QRect(10, 10, 631, 541)) self.bang.setObjectName("bang") self.bang.setColumnCount(b) self.bang.setRowCount(a) self.bang.setColumnWidth(0, 220) self.bang.setColumnWidth(1, 375) #tao checkbox for row in range(len(list)): checkbox = QtWidgets.QCheckBox() checkbox.setChecked(True) self.item = QtWidgets.QTableWidgetItem(str(list[row])) self.item.setCheckState(QtCore.Qt.CheckState.Checked) self.bang.setItem(row, 0, self.item) # menu nay no MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 848, 21)) self.menubar.setObjectName("menubar") self.menuChange_acc = QtWidgets.QMenu(self.menubar) self.menuChange_acc.setObjectName("menuChange_acc") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.menubar.addAction(self.menuChange_acc.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.batdau.setText(_translate("MainWindow", "Bắt đầu")) self.ketthuc.setText(_translate("MainWindow", "Tạm Dừng")) self.layacc.setText(_translate("MainWindow", "Lấy ra acc chưa chạy ")) self.label.setText(_translate("MainWindow", "Điền Luồng đầu tiên :")) __sortingEnabled = self.bang.isSortingEnabled() self.bang.setSortingEnabled(False) self.bang.setSortingEnabled(__sortingEnabled) self.menuChange_acc.setTitle(_translate("MainWindow", "Change acc")) # tạo tên cho cột và hàng column_labels = ['Tài khoản', 'Hoạt Động '] self.bang.setHorizontalHeaderLabels(column_labels) # cai luong mac dinh self.luong.setText("1") # self.batdau.clicked.connect(lambda: self.lay_list_chay(listacc)) self.ketthuc.clicked.connect(self.tam_dung) self.batdau.clicked.connect(self.change_info) def layluong(self): self.luongchay = self.luong.text() self.luongchay1 = int(self.luongchay) return self.luongchay1 def lay_list_checkstatic(self): self.listacc = [] for i in range(self.bang.rowCount()): item = self.bang.item(i, 0) # 0 is the column index of the checkbox column if item.checkState() == QtCore.Qt.CheckState.Checked: self.listacc.append(item) return self.listacc def tam_dung(self): if self.is_running: self.is_running = False self.ketthuc.setText("Tiếp Tục") print(self.is_running) else: self.is_running = True self.ketthuc.setText("Tạm Dừng") print(self.is_running) self.change_info() def change_info(self): if self.is_running == False: self.is_running = True self.ketthuc.setText("Tạm Dừng") self.chieudaicuaacc = len(self.lay_list_checkstatic()) self.soluong = self.layluong() if self.chieudaicuaacc % self.soluong != 0 : self.lanchay = int((self.chieudaicuaacc / self.soluong ))+1 else: self.lanchay = int(self.chieudaicuaacc / self.soluong ) if self.is_running == True: max_rows = self.soluong # Số lượng hàng tối đa cần lấy ra for a in range(self.lanchay): self.acc = self.lay_list_checkstatic() for b in range(self.soluong): try: print("jojojo") self.acc[b].setCheckState(QtCore.Qt.CheckState.Unchecked) except: pass self.tho[a] = Runner() self.tho[a].start() self.tho[a].signal.finish.connect(self.chaythu) #worker_1.signals.progressed.connect(receiver=,) if self.is_running == False: self.ketthuc.setText("Tiếp Tục") def chaythu(self,tu): print(tu) class Runner(QtCore.QThread): def __init__(self): super(Runner, self).__init__() self.signal = RunnerSignals() def run(self): for duc in range(1444): print("luong ",duc) print("xong 1 luong ") self.signal.finish.emit("g") if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() listacc, sohang = mo_file() ui.setupUi(MainWindow, sohang, 2,listacc) MainWindow.show() sys.exit(app.exec()) """ Is this what people want me to code in this direction? I tried but it didn't work. if possible please help me tthanks. my "file.txt" : 1|5e3omuqpwds 1|5e3omuqpwds 1|5e3omuqpwds 1|5e3omuqpwds 1|5e3omuqpwds """
-
my code : import mofbchange import sys , time from PyQt5 import QtCore, QtGui, QtWidgets def mo_file(): lines = [line.strip() for line in open("file.txt")] non_empty_lines = list(filter(lambda x: x, lines)) return non_empty_lines, len(non_empty_lines) class RunnerSignals(QtCore.QObject): progressed = QtCore.pyqtSignal(int) finish = QtCore.pyqtSignal(str) class Ui_MainWindow(object): def setupUi(self, MainWindow, a, b,list): self.is_running = True pool = QtCore.QThreadPool.globalInstance() MainWindow.setObjectName("MainWindow") MainWindow.resize(848, 595) self.centralwidget = QtWidgets.QWidget(MainWindow) self.centralwidget.setObjectName("centralwidget") self.batdau = QtWidgets.QPushButton(self.centralwidget) self.batdau.setGeometry(QtCore.QRect(660, 10, 131, 81)) self.batdau.setCursor(QtGui.QCursor(QtCore.Qt.CursorShape.ArrowCursor)) self.batdau.setAutoDefault(False) self.batdau.setObjectName("batdau") self.ketthuc = QtWidgets.QToolButton(self.centralwidget) self.ketthuc.setGeometry(QtCore.QRect(660, 100, 131, 71)) self.ketthuc.setObjectName("ketthuc") self.layacc = QtWidgets.QPushButton(self.centralwidget) self.layacc.setGeometry(QtCore.QRect(660, 180, 131, 81)) self.layacc.setObjectName("layacc") self.splitter = QtWidgets.QSplitter(self.centralwidget) self.splitter.setGeometry(QtCore.QRect(650, 320, 177, 20)) self.splitter.setOrientation(QtCore.Qt.Orientation.Horizontal) self.splitter.setObjectName("splitter") self.label = QtWidgets.QLabel(self.splitter) self.label.setObjectName("label") self.luong = QtWidgets.QLineEdit(self.splitter) self.luong.setObjectName("luong") self.tho = {} # tao bang self.bang = QtWidgets.QTableWidget(self.centralwidget) self.bang.setGeometry(QtCore.QRect(10, 10, 631, 541)) self.bang.setObjectName("bang") self.bang.setColumnCount(b) self.bang.setRowCount(a) self.bang.setColumnWidth(0, 220) self.bang.setColumnWidth(1, 375) #tao checkbox for row in range(len(list)): checkbox = QtWidgets.QCheckBox() checkbox.setChecked(True) self.item = QtWidgets.QTableWidgetItem(str(list[row])) self.item.setCheckState(QtCore.Qt.CheckState.Checked) self.bang.setItem(row, 0, self.item) # menu nay no MainWindow.setCentralWidget(self.centralwidget) self.menubar = QtWidgets.QMenuBar(MainWindow) self.menubar.setGeometry(QtCore.QRect(0, 0, 848, 21)) self.menubar.setObjectName("menubar") self.menuChange_acc = QtWidgets.QMenu(self.menubar) self.menuChange_acc.setObjectName("menuChange_acc") MainWindow.setMenuBar(self.menubar) self.statusbar = QtWidgets.QStatusBar(MainWindow) self.statusbar.setObjectName("statusbar") MainWindow.setStatusBar(self.statusbar) self.menubar.addAction(self.menuChange_acc.menuAction()) self.retranslateUi(MainWindow) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): _translate = QtCore.QCoreApplication.translate MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow")) self.batdau.setText(_translate("MainWindow", "Bắt đầu")) self.ketthuc.setText(_translate("MainWindow", "Tạm Dừng")) self.layacc.setText(_translate("MainWindow", "Lấy ra acc chưa chạy ")) self.label.setText(_translate("MainWindow", "Điền Luồng đầu tiên :")) __sortingEnabled = self.bang.isSortingEnabled() self.bang.setSortingEnabled(False) self.bang.setSortingEnabled(__sortingEnabled) self.menuChange_acc.setTitle(_translate("MainWindow", "Change acc")) # tạo tên cho cột và hàng column_labels = ['Tài khoản', 'Hoạt Động '] self.bang.setHorizontalHeaderLabels(column_labels) # cai luong mac dinh self.luong.setText("1") # self.batdau.clicked.connect(lambda: self.lay_list_chay(listacc)) self.ketthuc.clicked.connect(self.tam_dung) self.batdau.clicked.connect(self.change_info) def layluong(self): self.luongchay = self.luong.text() self.luongchay1 = int(self.luongchay) return self.luongchay1 def lay_list_checkstatic(self): self.listacc = [] for i in range(self.bang.rowCount()): item = self.bang.item(i, 0) # 0 is the column index of the checkbox column if item.checkState() == QtCore.Qt.CheckState.Checked: self.listacc.append(item) return self.listacc def tam_dung(self): if self.is_running: self.is_running = False self.ketthuc.setText("Tiếp Tục") print(self.is_running) else: self.is_running = True self.ketthuc.setText("Tạm Dừng") print(self.is_running) self.change_info() def change_info(self): if self.is_running == False: self.is_running = True self.ketthuc.setText("Tạm Dừng") self.chieudaicuaacc = len(self.lay_list_checkstatic()) self.soluong = self.layluong() if self.chieudaicuaacc % self.soluong != 0 : self.lanchay = int((self.chieudaicuaacc / self.soluong ))+1 else: self.lanchay = int(self.chieudaicuaacc / self.soluong ) if self.is_running == True: max_rows = self.soluong # Số lượng hàng tối đa cần lấy ra for a in range(self.lanchay): self.acc = self.lay_list_checkstatic() for b in range(self.soluong): try: print("jojojo") self.acc[b].setCheckState(QtCore.Qt.CheckState.Unchecked) except: pass self.tho[a] = Runner() self.tho[a].start() self.tho[a].signal.finish.connect(self.chaythu) #worker_1.signals.progressed.connect(receiver=,) if self.is_running == False: self.ketthuc.setText("Tiếp Tục") def chaythu(self,tu): print(tu) class Runner(QtCore.QThread): def __init__(self): super(Runner, self).__init__() self.signal = RunnerSignals() def run(self): for duc in range(1444): print("luong ",duc) print("xong 1 luong ") self.signal.finish.emit("g") if __name__ == "__main__": app = QtWidgets.QApplication(sys.argv) MainWindow = QtWidgets.QMainWindow() ui = Ui_MainWindow() listacc, sohang = mo_file() ui.setupUi(MainWindow, sohang, 2,listacc) MainWindow.show() sys.exit(app.exec()) """ Is this what people want me to code in this direction? I tried but it didn't work. if possible please help me tthanks. my "file.txt" : 1|5e3omuqpwds 1|5e3omuqpwds 1|5e3omuqpwds 1|5e3omuqpwds 1|5e3omuqpwds """
@khong-muon-them-nhieu-sai-lam said in waitForDone() I am not compatible with gui , gui is frozen:
I tried but it didn't work
Can you please also tell us in what way it "didn't work"? What happens now?
-
@khong-muon-them-nhieu-sai-lam said in waitForDone() I am not compatible with gui , gui is frozen:
I tried but it didn't work
Can you please also tell us in what way it "didn't work"? What happens now?
@jsulm
it doesn't freeze the gui . but it runs continuously and doesn't stop for thread A to finish and then thread B -
@jsulm
it doesn't freeze the gui . but it runs continuously and doesn't stop for thread A to finish and then thread B -
@khong-muon-them-nhieu-sai-lam hi, you are not chaining anything hence it can't work the way you expect them to.
On a side note, retranslateUI is not the place to the initialisation/connections you have put there.
Another note, your RunnerSignals class has no reason to exist and its usage is wrong. QThread is a QObject and you can define signals directly in it.
-
@JonB @jsulm @SGaist @mrjj @wrosecrans
Can you give me an example? because I'm really trying to follow everyone's wishes but it's not working. or give me a reference link but what i am dealing with . pls -
@JonB @jsulm @SGaist @mrjj @wrosecrans
Can you give me an example? because I'm really trying to follow everyone's wishes but it's not working. or give me a reference link but what i am dealing with . pls@khong-muon-them-nhieu-sai-lam
Told you to doself.worker1.signals.finished.connect(self.someMethodWhichStartsTheNextThread) # or you can use a Python *lambda* here
What have you done about connecting the starting of the second thread when the first thread finishes as above, show us where you have done this? Since you don't seem to have done so the second thread won't start. Don't know what "reference link" you are expecting, just implement it.
-
@khong-muon-them-nhieu-sai-lam
Told you to doself.worker1.signals.finished.connect(self.someMethodWhichStartsTheNextThread) # or you can use a Python *lambda* here
What have you done about connecting the starting of the second thread when the first thread finishes as above, show us where you have done this? Since you don't seem to have done so the second thread won't start. Don't know what "reference link" you are expecting, just implement it.
@JonB
The issue here is that I don't just run one specific thread, but I run a group of threads! The way you showed me only performs a certain thread, not a group of threads. I need to run a group of threads altogether and continue to run another group of threads after that. -
@JonB
The issue here is that I don't just run one specific thread, but I run a group of threads! The way you showed me only performs a certain thread, not a group of threads. I need to run a group of threads altogether and continue to run another group of threads after that.@khong-muon-them-nhieu-sai-lam
So apply the same principle except that you need to count/mark off as each thread finishes so that you will know when they have all finished, then spawn off the next group of threads when the last one in the first group finishes.