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

  • <small id='zP8wh'></small><noframes id='zP8wh'>

    <tfoot id='zP8wh'></tfoot>
      <bdo id='zP8wh'></bdo><ul id='zP8wh'></ul>

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

      1. <legend id='zP8wh'><style id='zP8wh'><dir id='zP8wh'><q id='zP8wh'></q></dir></style></legend>

        PyQt 啟動后進度躍升至 100%

        PyQt progress jumps to 100% after it starts(PyQt 啟動后進度躍升至 100%)

        • <small id='a2sg0'></small><noframes id='a2sg0'>

            <tbody id='a2sg0'></tbody>

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

                  本文介紹了PyQt 啟動后進度躍升至 100%的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  當我在 doWork 方法中運行代碼時,通過單擊 button1,進度條按預期工作.

                  但是,當我將列表從其他方法(即 btn2btn3)傳遞給 doWork 方法時,進度條只是跳躍啟動后達到 100%.

                  從 PyQt5 導入 QtCore、QtGui、QtWidgets從 PyQt5.QtWidgets 導入 *從 PyQt5.QtCore 導入 *從 PyQt5.QtGui 導入 *導入系統(tǒng)從硒導入網絡驅動程序SeleniumWorker 類(QtCore.QObject):progressChanged = QtCore.pyqtSignal(int)def doWork(self, lst=['http://www.somesite.com/','http://www.somesite.com/page2','http://www.somesite.com/page3']):進度 = 0瀏覽器 = webdriver.Firefox()鏈接 = lst對于鏈接中的鏈接:browser.get(鏈接)進度 += 100/len(鏈接)self.progressChanged.emit(進度)瀏覽器.close()類小部件(QtWidgets.QWidget):def __init__(self, *args, **kwargs):QtWidgets.QWidget.__init__(self, *args, **kwargs)放置 = QtWidgets.QHBoxLayout(self)progressBar = QtWidgets.QProgressBar()進度條.setRange(0, 100)button1 = QtWidgets.QPushButton("Start1")button2 = QtWidgets.QPushButton("Start2")button3 = QtWidgets.QPushButton("Start3")放置.addWidget(進度條)放置.addWidget(button1)放置.addWidget(按鈕2)放置.addWidget(button3)self.thread = QtCore.QThread()self.worker = SeleniumWorker()self.worker.moveToThread(self.thread)self.thread.started.connect(self.worker.doWork)button1.clicked.connect(self.thread.start)button2.clicked.connect(self.btn2)button3.clicked.connect(self.btn3)self.worker.progressChanged.connect(progressBar.setValue)def btn2(自我):self.lst2 = ['http://www.somesite.com/page4','http://www.somesite.com/page5','http://www.somesite.com/page6']self.worker.doWork(self.lst2)def btn3(自我):self.lst3 = ['http://www.somesite.com/page7','http://www.somesite.com/page8','http://www.somesite.com/page9']self.worker.doWork(self.lst3)如果 __name__ == '__main__':應用程序 = QtWidgets.QApplication(sys.argv)w = 小部件()w.show()sys.exit(app.exec_())

                  解決方案

                  看來你沒有看懂我的邏輯以前的解決方案,我會詳細說明過程:

                  [1] self.thread = QtCore.QThread()[2] self.worker = SeleniumWorker()[3] self.worker.moveToThread(self.thread)[4] self.worker.progressChanged.connect(progressBar.setValue, QtCore.Qt.QueuedConnection)[5] self.thread.started.connect(self.worker.doWork)

                  1. QThread 是一個線程處理程序,因此該類的對象允許我們在主線程以外的稱為 GUI 線程的線程上執(zhí)行任務.

                    李>
                  2. 您正在創(chuàng)建一個具有 doWork 方法的 SeleniumWorker 對象,這是一個不應在 GUI 線程中執(zhí)行的阻塞任務,而這將通過之前的 QThread 來實現(xiàn).

                  3. 由于函數(shù)必須在另一個線程中執(zhí)行,具有該方法的對象必須移動到另一個線程.

                  4. 另一個線程中的對象的信號連接到GUI線程中的QProgressBar,這個連接必須使用標志QtCore.Qt.QueuedConnection.

                  5. 當線程啟動時,它會調用 doWork 函數(shù),因為 self.worker 對象在另一個線程中,所以該函數(shù)也將在另一個線程中執(zhí)行.

                  <小時>

                  在下一部分顯示的代碼中,您在線程尚未啟動時調用 doWork 函數(shù),因此它將在主線程中執(zhí)行.

                  def btn2(self):...# 主線程self.worker.doWork(self.lst2)

                  <小時>

                  傳遞 url 的一種方法是通過 setter 方法,然后啟動線程.線程啟動時會調用doWork,當doWork執(zhí)行時會發(fā)出progressChanged信號.

                  通過以上所有內容,我們獲得以下內容:

                  導入系統(tǒng)從 PyQt5 導入 QtCore、QtGui、QtWidgets從硒導入網絡驅動程序SeleniumWorker 類(QtCore.QObject):progressChanged = QtCore.pyqtSignal(int)開始 = QtCore.pyqtSignal()完成 = QtCore.pyqtSignal()def setUrls(self, urls=['http://www.somesite.com/','http://www.somesite.com/page2','http://www.somesite.com/page3']):self.urls = 網址定義做工作(自我):self.started.emit()進度 = 0self.progressChanged.emit(進度)瀏覽器 = webdriver.Firefox()鏈接 = self.urls對于鏈接中的鏈接:browser.get(鏈接)進度 += 100/len(鏈接)self.progressChanged.emit(進度)瀏覽器.close()self.finished.emit()類小部件(QtWidgets.QWidget):def __init__(self, *args, **kwargs):QtWidgets.QWidget.__init__(self, *args, **kwargs)放置 = QtWidgets.QHBoxLayout(self)self.progressBar = QtWidgets.QProgressBar()self.progressBar.setRange(0, 100)button1 = QtWidgets.QPushButton("Start1")button2 = QtWidgets.QPushButton("Start2")button3 = QtWidgets.QPushButton("Start3")放置.addWidget(self.progressBar)放置.addWidget(button1)放置.addWidget(按鈕2)放置.addWidget(button3)self.thread = QtCore.QThread()self.worker = SeleniumWorker()self.worker.moveToThread(self.thread)self.worker.progressChanged.connect(self.progressBar.setValue, QtCore.Qt.QueuedConnection)self.thread.started.connect(self.worker.doWork)button1.clicked.connect(self.btn1)button2.clicked.connect(self.btn2)button3.clicked.connect(self.btn3)self.worker.finished.connect(self.on_finished)self.worker.started.connect(lambda: self.buttons_setEnable(False))def on_finished(self):self.buttons_setEnable(True)如果 self.thread.isRunning():self.thread.quit()self.thread.wait()def button_setEnable(self, enable):對于self.findChildren(QtWidgets.QPushButton)中的btn:btn.setEnabled(啟用)def btn1(自我):self.worker.setUrls()self.thread.start()def btn2(自我):lst2 = ['http://www.somesite.com/page4','http://www.somesite.com/page5','http://www.somesite.com/page6']self.worker.setUrls(lst2)self.thread.start()def btn3(自我):lst3 = ['http://www.somesite.com/page7','http://www.somesite.com/page8','http://www.somesite.com/page9']self.worker.setUrls(lst3)self.thread.start()如果 __name__ == '__main__':應用程序 = QtWidgets.QApplication(sys.argv)w = 小部件()w.show()sys.exit(app.exec_())

                  注意:我添加了在運行時禁用按鈕的功能,因為線程在工作時不會再次啟動是合適的.p>

                  When I run the code in the in the doWork method, by clicking the button1, the progress bar works as expected.

                  However, when I pass the list to the doWork method from other methods (i.e. btn2, btn3), the progress bar just jumps to 100% after it starts.

                  from PyQt5 import QtCore, QtGui, QtWidgets
                  from PyQt5.QtWidgets import *
                  from PyQt5.QtCore import *
                  from PyQt5.QtGui import *
                  import sys
                  from selenium import webdriver
                  
                  class SeleniumWorker(QtCore.QObject):
                      progressChanged = QtCore.pyqtSignal(int)
                      def doWork(self, lst=['http://www.somesite.com/',
                          'http://www.somesite.com/page2',
                          'http://www.somesite.com/page3']):
                          progress = 0
                          browser = webdriver.Firefox()
                          links = lst
                          for link in links:
                              browser.get(link)
                              progress += 100 / len(links)
                              self.progressChanged.emit(progress)
                          browser.close()
                  
                  class Widget(QtWidgets.QWidget):
                      def __init__(self, *args, **kwargs):
                          QtWidgets.QWidget.__init__(self, *args, **kwargs)
                          lay = QtWidgets.QHBoxLayout(self)
                          progressBar = QtWidgets.QProgressBar()
                          progressBar.setRange(0, 100)
                          button1 = QtWidgets.QPushButton("Start1")
                          button2 = QtWidgets.QPushButton("Start2")
                          button3 = QtWidgets.QPushButton("Start3")
                          lay.addWidget(progressBar)
                          lay.addWidget(button1)
                          lay.addWidget(button2)
                          lay.addWidget(button3)
                          self.thread = QtCore.QThread()
                          self.worker = SeleniumWorker()
                          self.worker.moveToThread(self.thread)
                          self.thread.started.connect(self.worker.doWork)
                          button1.clicked.connect(self.thread.start)
                          button2.clicked.connect(self.btn2)
                          button3.clicked.connect(self.btn3)
                          self.worker.progressChanged.connect(progressBar.setValue)
                  
                  
                      def btn2(self):
                          self.lst2 = ['http://www.somesite.com/page4',
                          'http://www.somesite.com/page5',
                          'http://www.somesite.com/page6']
                          self.worker.doWork(self.lst2)
                  
                      def btn3(self):
                          self.lst3 = ['http://www.somesite.com/page7',
                          'http://www.somesite.com/page8',
                          'http://www.somesite.com/page9']
                          self.worker.doWork(self.lst3)
                  
                  
                  if __name__ == '__main__':
                      app = QtWidgets.QApplication(sys.argv)
                      w = Widget()
                      w.show()
                      sys.exit(app.exec_())
                  

                  解決方案

                  It seems that you have not understood the logic of my previous solution, I will detail the procedure:

                  [1] self.thread = QtCore.QThread()
                  [2] self.worker = SeleniumWorker()
                  [3] self.worker.moveToThread(self.thread)
                  [4] self.worker.progressChanged.connect(progressBar.setValue, QtCore.Qt.QueuedConnection)
                  [5] self.thread.started.connect(self.worker.doWork)
                  

                  1. QThread is a thread handler, so an object of that class allows us to execute a task on a thread other than the main thread called the GUI thread.

                  2. You are creating a SeleniumWorker object that has the doWork method, which is a blocking task that should not be executed in the GUI thread, and that will be achieved with the previous QThread.

                  3. Since the function must be executed in another thread, the object that has that method must move to that other thread.

                  4. The signal of the object that is in the other thread is connected to the QProgressBar that is in the GUI thread, this connection must be made with the flag QtCore.Qt.QueuedConnection.

                  5. When the thread is started it will call the doWork function and since the self.worker object is in another thread then the function will also be executed in that other thread.


                  In your case in the code that is shown in the next part you are calling the doWork function when the thread has not yet started so it will be executed in the main thread.

                  def btn2(self):
                      ...
                      # main thread
                      self.worker.doWork(self.lst2)
                  


                  One way to pass the urls is through a setter method, and then start the thread. When the thread starts, it will call doWork, and when the doWork is executed, the progressChanged signal will be emited.

                  With all the above we obtain the following:

                  import sys
                  
                  from PyQt5 import QtCore, QtGui, QtWidgets
                  from selenium import webdriver
                  
                  class SeleniumWorker(QtCore.QObject):
                      progressChanged = QtCore.pyqtSignal(int)
                      started = QtCore.pyqtSignal()
                      finished = QtCore.pyqtSignal()
                      def setUrls(self, urls=['http://www.somesite.com/',
                      'http://www.somesite.com/page2',
                      'http://www.somesite.com/page3']):
                          self.urls = urls
                  
                      def doWork(self):
                          self.started.emit()
                          progress = 0
                          self.progressChanged.emit(progress)
                          browser = webdriver.Firefox()
                          links = self.urls
                          for link in links:
                              browser.get(link)
                              progress += 100 / len(links)
                              self.progressChanged.emit(progress)
                          browser.close()
                          self.finished.emit()
                  
                  class Widget(QtWidgets.QWidget):
                      def __init__(self, *args, **kwargs):
                          QtWidgets.QWidget.__init__(self, *args, **kwargs)
                          lay = QtWidgets.QHBoxLayout(self)
                          self.progressBar = QtWidgets.QProgressBar()
                          self.progressBar.setRange(0, 100)
                          button1 = QtWidgets.QPushButton("Start1")
                          button2 = QtWidgets.QPushButton("Start2")
                          button3 = QtWidgets.QPushButton("Start3")
                          lay.addWidget(self.progressBar)
                          lay.addWidget(button1)
                          lay.addWidget(button2)
                          lay.addWidget(button3)
                          self.thread = QtCore.QThread()
                          self.worker = SeleniumWorker()
                          self.worker.moveToThread(self.thread)
                          self.worker.progressChanged.connect(self.progressBar.setValue, QtCore.Qt.QueuedConnection)
                          self.thread.started.connect(self.worker.doWork)
                          button1.clicked.connect(self.btn1)
                          button2.clicked.connect(self.btn2)
                          button3.clicked.connect(self.btn3)
                          self.worker.finished.connect(self.on_finished)
                          self.worker.started.connect(lambda: self.buttons_setEnable(False))
                  
                      def on_finished(self):
                          self.buttons_setEnable(True)
                          if self.thread.isRunning():
                              self.thread.quit()
                              self.thread.wait()
                  
                      def buttons_setEnable(self, enable):
                          for btn in self.findChildren(QtWidgets.QPushButton):
                              btn.setEnabled(enable)
                  
                      def btn1(self):
                          self.worker.setUrls()
                          self.thread.start()
                  
                      def btn2(self):
                          lst2 = ['http://www.somesite.com/page4',
                          'http://www.somesite.com/page5',
                          'http://www.somesite.com/page6']
                          self.worker.setUrls(lst2)
                          self.thread.start()
                  
                      def btn3(self):
                          lst3 = ['http://www.somesite.com/page7',
                          'http://www.somesite.com/page8',
                          'http://www.somesite.com/page9']
                          self.worker.setUrls(lst3)
                          self.thread.start()
                  
                  
                  if __name__ == '__main__':
                      app = QtWidgets.QApplication(sys.argv)
                      w = Widget()
                      w.show()
                      sys.exit(app.exec_())
                  

                  Note: I have added the functionality of disabling the buttons while it is running as it is appropriate that the thread does not start again while it is working.

                  這篇關于PyQt 啟動后進度躍升至 100%的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

                  相關文檔推薦

                  How to bind a function to an Action from Qt menubar?(如何將函數(shù)綁定到 Qt 菜單欄中的操作?)
                  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` 構造函數(shù)有未知關鍵字 `data`)
                  Change x-axis ticks to custom strings(將 x 軸刻度更改為自定義字符串)
                  How to show progress bar while saving file to excel in python?(如何在python中將文件保存為excel時顯示進度條?)
                  Erasing pen on a canvas(在畫布上擦筆)
                    <bdo id='qLB0D'></bdo><ul id='qLB0D'></ul>

                      <legend id='qLB0D'><style id='qLB0D'><dir id='qLB0D'><q id='qLB0D'></q></dir></style></legend>

                      • <tfoot id='qLB0D'></tfoot>

                          <tbody id='qLB0D'></tbody>
                        • <small id='qLB0D'></small><noframes id='qLB0D'>

                            <i id='qLB0D'><tr id='qLB0D'><dt id='qLB0D'><q id='qLB0D'><span id='qLB0D'><b id='qLB0D'><form id='qLB0D'><ins id='qLB0D'></ins><ul id='qLB0D'></ul><sub id='qLB0D'></sub></form><legend id='qLB0D'></legend><bdo id='qLB0D'><pre id='qLB0D'><center id='qLB0D'></center></pre></bdo></b><th id='qLB0D'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='qLB0D'><tfoot id='qLB0D'></tfoot><dl id='qLB0D'><fieldset id='qLB0D'></fieldset></dl></div>
                            主站蜘蛛池模板: 国产一区二区三区在线视频 | 免费福利片 | 中文字幕在线日韩 | 中文字幕在线免费观看 | 欧美黄色片视频 | 成人午夜在线视频 | 亚洲国产中文字幕 | 在线观看日韩av | 日本福利视频 | 深夜视频在线观看 | 日韩成人在线免费观看 | 毛片www| 日韩亚洲一区二区 | 国产精品日韩欧美 | 国产一区二区三区视频在线 | 日本视频免费观看 | 亚洲精品视频在线观看免费 | 日韩免费高清视频 | 91网在线 | 国产精品毛片一区视频播 | 欧美在线视频一区二区 | 综合色在线 | 长河落日连续剧48集免费观看 | 国产午夜在线 | 在线播放一区 | 国产成人在线免费观看 | 亚洲另类视频 | 欧美日视频 | 91视频亚洲 | 亚洲黄色小视频 | 欧美 日韩 国产 成人 在线 | 精品乱子伦一区二区三区 | 免费性爱视频 | 久久激情小说 | 色片网址 | 国产乱淫av片免费 | 青青国产在线 | 91看片在线 | av免费播放| 日韩黄色片 | 日韩精品欧美 |