久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    <small id='X9GHC'></small><noframes id='X9GHC'>

    <tfoot id='X9GHC'></tfoot>

      • <bdo id='X9GHC'></bdo><ul id='X9GHC'></ul>
    1. <legend id='X9GHC'><style id='X9GHC'><dir id='X9GHC'><q id='X9GHC'></q></dir></style></legend>
      <i id='X9GHC'><tr id='X9GHC'><dt id='X9GHC'><q id='X9GHC'><span id='X9GHC'><b id='X9GHC'><form id='X9GHC'><ins id='X9GHC'></ins><ul id='X9GHC'></ul><sub id='X9GHC'></sub></form><legend id='X9GHC'></legend><bdo id='X9GHC'><pre id='X9GHC'><center id='X9GHC'></center></pre></bdo></b><th id='X9GHC'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='X9GHC'><tfoot id='X9GHC'></tfoot><dl id='X9GHC'><fieldset id='X9GHC'></fieldset></dl></div>
    2. Pyqt5 qthread + 信號不工作 + gui 凍結

      Pyqt5 qthread + signal not working + gui freeze(Pyqt5 qthread + 信號不工作 + gui 凍結)
      • <bdo id='ZCHDe'></bdo><ul id='ZCHDe'></ul>

          • <tfoot id='ZCHDe'></tfoot>
              <tbody id='ZCHDe'></tbody>
            <legend id='ZCHDe'><style id='ZCHDe'><dir id='ZCHDe'><q id='ZCHDe'></q></dir></style></legend>

              <small id='ZCHDe'></small><noframes id='ZCHDe'>

              <i id='ZCHDe'><tr id='ZCHDe'><dt id='ZCHDe'><q id='ZCHDe'><span id='ZCHDe'><b id='ZCHDe'><form id='ZCHDe'><ins id='ZCHDe'></ins><ul id='ZCHDe'></ul><sub id='ZCHDe'></sub></form><legend id='ZCHDe'></legend><bdo id='ZCHDe'><pre id='ZCHDe'><center id='ZCHDe'></center></pre></bdo></b><th id='ZCHDe'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='ZCHDe'><tfoot id='ZCHDe'></tfoot><dl id='ZCHDe'><fieldset id='ZCHDe'></fieldset></dl></div>

              1. 本文介紹了Pyqt5 qthread + 信號不工作 + gui 凍結的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                問題描述

                我正在嘗試使用 imap lib 制作郵箱檢查器,它在沒有 gui 的情況下與 python、隊列和多線程一起工作得很好.

                I am trying to make a mailbox checker with imap lib, it work pretty fine with python, queue and multithread without gui.

                但是當我嘗試放置一個 gui 時,我所做的每一個功能,都讓 gui 凍結直到完成.

                But when I try to put a gui, every fonction i made, make the gui freeze until finish .

                我從各種文檔(添加 qthread、signal、cursor 等)和教程中嘗試了很多東西,但對我沒有用.

                I tried many thing from various doc(add qthread, signal, cursorr etcc) and tutorials none worked for me .

                有人可以幫助我了解如何在運行函數時將文本設置或附加到 QtextEdit,因為它只有在完成后才能工作.

                Can someone help me to understand how to set or append a text to a QtextEdit while running a function coz it work only after finish .

                這是我的代碼:

                class Checker(QtCore.QThread):
                    signal = QtCore.pyqtSignal(object)
                
                    def __init__(self, lignesmailtocheck):
                        QtCore.QThread.__init__(self)
                        self.lignesmailtocheck = lignesmailtocheck
                
                    def run(self):
                            lignemailtocheck = self.lignesmailtocheck.strip()                        
                            maillo, passo = lignemailtocheck.split(":",1)
                            debmail, finmail = maillo.split("@",1)
                            setimap =["oultook.com:imap-mail.outlook.com", "gmail.com:imap.gmail.com"]
                            for lignesimaptocheck in sorted(setimap):
                                    ligneimaptocheck = lignesimaptocheck.strip()
                                    fai, imap = ligneimaptocheck.split(":",1)                                
                                    if finmail == fai:
                                            passo0 = passo.rstrip()
                                            try :
                                                    mail = imaplib.IMAP4_SSL(imap)
                                                    mail.login(maillo, passo)
                                                    mailboxok = open("MailBoxOk.txt", "a+", encoding='utf-8', errors='ignore')
                                                    mailboxok.write(maillo+":"+passo+"
                ")
                                                    mailboxok.close()
                                                    totaly = maillo+":"+passo0+":"+imap                                
                                                    print(maillo+":"+passo+"
                ")
                
                                                    self.send_text.emit(totaly)
                                                    time.sleep(1)
                                            except imaplib.IMAP4.error:                          
                                                           print ("LOGIN FAILED!!! ")
                class Ui_Form(object):
                    def setupUi(self, Form):
                        Form.setObjectName("Form")
                        Form.resize(400, 300)
                
                        self.pushButton = QtWidgets.QPushButton(Form)
                        self.pushButton.setGeometry(QtCore.QRect(150, 210, 75, 23))
                        self.pushButton.setObjectName("pushButton")
                        self.pushButton.clicked.connect(self.gogogo)
                
                        self.openliste = QtWidgets.QToolButton(Form)
                        self.openliste.setGeometry(QtCore.QRect(40, 110, 71, 21))
                        self.openliste.setObjectName("openliste")
                
                        self.textEdit = QtWidgets.QTextEdit(Form)
                        self.textEdit.setGeometry(QtCore.QRect(170, 50, 201, 121))
                        self.textEdit.setObjectName("textEdit")
                
                        self.progressBar = QtWidgets.QProgressBar(Form)
                        self.progressBar.setGeometry(QtCore.QRect(10, 260, 381, 23))
                        self.progressBar.setValue(0)
                        self.progressBar.setObjectName("progressBar")
                
                        self.retranslateUi(Form)
                        QtCore.QMetaObject.connectSlotsByName(Form)
                
                    def retranslateUi(self, Form):
                        _translate = QtCore.QCoreApplication.translate
                        Form.setWindowTitle(_translate("Form", "Form"))
                        self.pushButton.setText(_translate("Form", "PushButton"))
                        self.openliste.setText(_translate("Form", "..."))
                
                    def gogogo(self):
                
                        mailtocheck = open('File/toCheck.txt', 'r', encoding='utf-8', errors='ignore').readlines()        
                        setmailtocheck = set(mailtocheck)
                        for lignesmailtocheck in sorted(setmailtocheck):
                            checker = Checker(lignesmailtocheck)
                
                            thread = QThread()
                            checker.moveToThread(thread)
                            # connections after move so cross-thread:
                            thread.started.connect(checker.run)
                            checker.signal.connect(self.checkedok)
                            thread.start()
                
                    def checkedok(self, data):
                        print(data)
                        self.textEdit.append(data)
                if __name__ == "__main__":
                    app = QtWidgets.QApplication(sys.argv)
                    Form = QtWidgets.QWidget()
                    ui = Ui_Form()
                    ui.setupUi(Form)
                    Form.show()
                    sys.exit(app.exec_())
                

                推薦答案

                由于在PyQt中使用QThread經常會遇到問題,和你的類似,這里舉個例子說明如何正確使用線程在 PyQt 中.我希望它可以作為類似問題的轉到答案有用,所以我花了比平時更多的時間來準備這個.

                Since there are often questions about using QThread in PyQt, similar to yours, here is an example that shows how to correctly use threads in PyQt. I'm hoping it can be useful as a goto-answer for similar questions so I spent a bit more time than usual preparing this.

                該示例創建了許多在非主線程中執行的工作對象,并通過 Qt 的異步信號與主(即 GUI)線程進行通信.

                The example creates a number of worker objects that execute in non-main threads and communicate with the main (ie GUI) thread via Qt's asynchronous signals.

                import time
                import sys
                
                from PyQt5.QtCore import QObject, QThread, pyqtSignal, pyqtSlot
                from PyQt5.QtWidgets import QApplication, QPushButton, QTextEdit, QVBoxLayout, QWidget
                
                
                def trap_exc_during_debug(*args):
                    # when app raises uncaught exception, print info
                    print(args)
                
                
                # install exception hook: without this, uncaught exception would cause application to exit
                sys.excepthook = trap_exc_during_debug
                
                
                class Worker(QObject):
                    """
                    Must derive from QObject in order to emit signals, connect slots to other signals, and operate in a QThread.
                    """
                
                    sig_step = pyqtSignal(int, str)  # worker id, step description: emitted every step through work() loop
                    sig_done = pyqtSignal(int)  # worker id: emitted at end of work()
                    sig_msg = pyqtSignal(str)  # message to be shown to user
                
                    def __init__(self, id: int):
                        super().__init__()
                        self.__id = id
                        self.__abort = False
                
                    @pyqtSlot()
                    def work(self):
                        """
                        Pretend this worker method does work that takes a long time. During this time, the thread's
                        event loop is blocked, except if the application's processEvents() is called: this gives every
                        thread (incl. main) a chance to process events, which in this sample means processing signals
                        received from GUI (such as abort).
                        """
                        thread_name = QThread.currentThread().objectName()
                        thread_id = int(QThread.currentThreadId())  # cast to int() is necessary
                        self.sig_msg.emit('Running worker #{} from thread "{}" (#{})'.format(self.__id, thread_name, thread_id))
                
                        for step in range(100):
                            time.sleep(0.1)
                            self.sig_step.emit(self.__id, 'step ' + str(step))
                
                            # check if we need to abort the loop; need to process events to receive signals;
                            app.processEvents()  # this could cause change to self.__abort
                            if self.__abort:
                                # note that "step" value will not necessarily be same for every thread
                                self.sig_msg.emit('Worker #{} aborting work at step {}'.format(self.__id, step))
                                break
                
                        self.sig_done.emit(self.__id)
                
                    def abort(self):
                        self.sig_msg.emit('Worker #{} notified to abort'.format(self.__id))
                        self.__abort = True
                
                
                class MyWidget(QWidget):
                    NUM_THREADS = 5
                
                    # sig_start = pyqtSignal()  # needed only due to PyCharm debugger bug (!)
                    sig_abort_workers = pyqtSignal()
                
                    def __init__(self):
                        super().__init__()
                
                        self.setWindowTitle("Thread Example")
                        form_layout = QVBoxLayout()
                        self.setLayout(form_layout)
                        self.resize(400, 800)
                
                        self.button_start_threads = QPushButton()
                        self.button_start_threads.clicked.connect(self.start_threads)
                        self.button_start_threads.setText("Start {} threads".format(self.NUM_THREADS))
                        form_layout.addWidget(self.button_start_threads)
                
                        self.button_stop_threads = QPushButton()
                        self.button_stop_threads.clicked.connect(self.abort_workers)
                        self.button_stop_threads.setText("Stop threads")
                        self.button_stop_threads.setDisabled(True)
                        form_layout.addWidget(self.button_stop_threads)
                
                        self.log = QTextEdit()
                        form_layout.addWidget(self.log)
                
                        self.progress = QTextEdit()
                        form_layout.addWidget(self.progress)
                
                        QThread.currentThread().setObjectName('main')  # threads can be named, useful for log output
                        self.__workers_done = None
                        self.__threads = None
                
                    def start_threads(self):
                        self.log.append('starting {} threads'.format(self.NUM_THREADS))
                        self.button_start_threads.setDisabled(True)
                        self.button_stop_threads.setEnabled(True)
                
                        self.__workers_done = 0
                        self.__threads = []
                        for idx in range(self.NUM_THREADS):
                            worker = Worker(idx)
                            thread = QThread()
                            thread.setObjectName('thread_' + str(idx))
                            self.__threads.append((thread, worker))  # need to store worker too otherwise will be gc'd
                            worker.moveToThread(thread)
                
                            # get progress messages from worker:
                            worker.sig_step.connect(self.on_worker_step)
                            worker.sig_done.connect(self.on_worker_done)
                            worker.sig_msg.connect(self.log.append)
                
                            # control worker:
                            self.sig_abort_workers.connect(worker.abort)
                
                            # get read to start worker:
                            # self.sig_start.connect(worker.work)  # needed due to PyCharm debugger bug (!); comment out next line
                            thread.started.connect(worker.work)
                            thread.start()  # this will emit 'started' and start thread's event loop
                
                        # self.sig_start.emit()  # needed due to PyCharm debugger bug (!)
                
                    @pyqtSlot(int, str)
                    def on_worker_step(self, worker_id: int, data: str):
                        self.log.append('Worker #{}: {}'.format(worker_id, data))
                        self.progress.append('{}: {}'.format(worker_id, data))
                
                    @pyqtSlot(int)
                    def on_worker_done(self, worker_id):
                        self.log.append('worker #{} done'.format(worker_id))
                        self.progress.append('-- Worker {} DONE'.format(worker_id))
                        self.__workers_done += 1
                        if self.__workers_done == self.NUM_THREADS:
                            self.log.append('No more workers active')
                            self.button_start_threads.setEnabled(True)
                            self.button_stop_threads.setDisabled(True)
                            # self.__threads = None
                
                    @pyqtSlot()
                    def abort_workers(self):
                        self.sig_abort_workers.emit()
                        self.log.append('Asking each worker to abort')
                        for thread, worker in self.__threads:  # note nice unpacking by Python, avoids indexing
                            thread.quit()  # this will quit **as soon as thread event loop unblocks**
                            thread.wait()  # <- so you need to wait for it to *actually* quit
                
                        # even though threads have exited, there may still be messages on the main thread's
                        # queue (messages that threads emitted before the abort):
                        self.log.append('All threads exited')
                
                
                if __name__ == "__main__":
                    app = QApplication([])
                
                    form = MyWidget()
                    form.show()
                
                    sys.exit(app.exec_())
                

                理解 PyQt 中多線程編程所必需的主要概念如下:

                The main concepts necessary to understand multi-thread programming in PyQt are the following:

                • Qt 線程有自己的事件循環(特定于每個線程).主線程,也就是 GUI 線程,也是一個 QThread,它的事件循環由那個線程管理.
                • 線程之間的信號通過接收線程的事件循環(異步)傳輸.因此,GUI 或任何線程的響應能力 = 處理事件的能力.例如,如果一個線程在函數循環中忙,它就不能處理事件,所以在函數返回之前它不會響應來自 GUI 的信號.
                • 如果線程中的工作對象(方法)可能必須根據來自 GUI 的信號更改其操作過程(例如,中斷循環或等待),它必須調用 processEvents()QApplication 實例上的 code>.這將允許 QThread 處理事件,從而調用槽以響應來自 GUI 的異步信號.請注意,QApplication.instance().processEvents() 似乎在每個線程上調用 processEvents(),如果不需要,則使用 QThread.currentThread().processEvents() 是一個有效的替代方案.
                • QThread.quit() 的調用不會立即退出其事件循環:它必須等待當前正在執行的槽(如果有)返回.因此,一旦一個線程被告知要退出,您必須在其上等待().因此,中止工作線程通常涉及(通過自定義信號)發出信號以停止它正在做的任何事情:這需要 GUI 對象上的自定義信號,將該信號連接到工作槽,并且工作方法必須調用線程的 processEvents() 以允許發出的信號在工作時到達插槽.
                • Qt threads have their own event loop (specific to each thread). The main thread, aka the GUI thread, is also a QThread, and its event loop is managed by that thread.
                • Signals between threads are transmitted (asynchronously) via the receiving thread's event loop. Hence responsiveness of GUI or any thread = ability to process events. E.g., if a thread is busy in a function loop, it can't process events, so it won't respond to signals from the GUI until the function returns.
                • If a worker object (method) in a thread may have to change its course of action based on signals from the GUI (say, to interrupt a loop or a wait), it must call processEvents() on the QApplication instance. This will allow the QThread to process events, and hence to call slots in response to async signals from the GUI. Note that QApplication.instance().processEvents() seems to call processEvents() on every thread, if this is not desired then QThread.currentThread().processEvents() is a valid alternative.
                • A call to QThread.quit() does not immediately quit its event loop: it must wait for currently executing slot (if any) to return. Hence once a thread is told to quit, you must wait() on it. So aborting a worker thread usually involves signaling it (via a custom signal) to stop whatever it is doing: this requires a custom signal on a GUI object, a connection of that signal to a worker slot, and worker work method must call thread's processEvents() to allow the emitted signal to reach the slot while doing work.

                這篇關于Pyqt5 qthread + 信號不工作 + gui 凍結的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                相關文檔推薦

                How to bind a function to an Action from Qt menubar?(如何將函數綁定到 Qt 菜單欄中的操作?)
                PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)
                How to set yaxis tick label in a fixed position so that when i scroll left or right the yaxis tick label should be visible?(如何將 yaxis 刻度標簽設置在固定位置,以便當我向左或向右滾動時,yaxis 刻度標簽應該可見
                `QImage` constructor has unknown keyword `data`(`QImage` 構造函數有未知關鍵字 `data`)
                Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)

              2. <small id='0qsRX'></small><noframes id='0qsRX'>

                  • <bdo id='0qsRX'></bdo><ul id='0qsRX'></ul>
                      <tbody id='0qsRX'></tbody>

                      <legend id='0qsRX'><style id='0qsRX'><dir id='0qsRX'><q id='0qsRX'></q></dir></style></legend>
                        1. <i id='0qsRX'><tr id='0qsRX'><dt id='0qsRX'><q id='0qsRX'><span id='0qsRX'><b id='0qsRX'><form id='0qsRX'><ins id='0qsRX'></ins><ul id='0qsRX'></ul><sub id='0qsRX'></sub></form><legend id='0qsRX'></legend><bdo id='0qsRX'><pre id='0qsRX'><center id='0qsRX'></center></pre></bdo></b><th id='0qsRX'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0qsRX'><tfoot id='0qsRX'></tfoot><dl id='0qsRX'><fieldset id='0qsRX'></fieldset></dl></div>
                          <tfoot id='0qsRX'></tfoot>

                          主站蜘蛛池模板: 欧美aⅴ| 亚洲精品一区二 | 青青草网站在线观看 | 精品国产区 | 日韩在线一区二区 | 日韩久久精品 | 岛国在线免费观看 | 刘亦菲国产毛片bd | 免费观看黄色一级片 | 亚洲一级av毛片 | 国内成人免费视频 | 日韩一二区在线 | 成人免费观看男女羞羞视频 | 国产在线观看一区二区 | 久久国产精品久久久久久久久久 | 一区欧美 | 你懂的免费在线 | 夜夜爆操 | 亚洲www啪成人一区二区 | 午夜视频在线免费观看 | 中文字幕av一区二区三区 | 亚洲精品一区中文字幕乱码 | 成人免费视频在线观看 | 性国产xxxx乳高跟 | 亚洲综合国产 | 午夜黄色影院 | 中文字幕av中文字幕 | 国产一区二区三区视频 | 国产精品日韩 | 老牛影视av一区二区在线观看 | 国产一二三区精品视频 | 色视频网站免费 | 久久精品99 | 久久精品国产久精国产 | 先锋资源吧 | 激情网五月天 | 久久久视 | 精品美女视频在线观看免费软件 | 久久黄网 | jav成人av免费播放 | 国产精品无码专区在线观看 |