waitForDone() I am not compatible with gui , gui is frozen
-
@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.