• <legend id='ubw6T'><style id='ubw6T'><dir id='ubw6T'><q id='ubw6T'></q></dir></style></legend>
      <bdo id='ubw6T'></bdo><ul id='ubw6T'></ul>
  • <tfoot id='ubw6T'></tfoot>

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

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

      1. 当我将 PyQt QProgressBar 与 Selenuim 一起使用时,它不

        时间:2023-08-06
          <tbody id='9Htl3'></tbody>

            <bdo id='9Htl3'></bdo><ul id='9Htl3'></ul>
          • <small id='9Htl3'></small><noframes id='9Htl3'>

            <i id='9Htl3'><tr id='9Htl3'><dt id='9Htl3'><q id='9Htl3'><span id='9Htl3'><b id='9Htl3'><form id='9Htl3'><ins id='9Htl3'></ins><ul id='9Htl3'></ul><sub id='9Htl3'></sub></form><legend id='9Htl3'></legend><bdo id='9Htl3'><pre id='9Htl3'><center id='9Htl3'></center></pre></bdo></b><th id='9Htl3'></th></span></q></dt></tr></i><div id='9Htl3'><tfoot id='9Htl3'></tfoot><dl id='9Htl3'><fieldset id='9Htl3'></fieldset></dl></div>
            • <tfoot id='9Htl3'></tfoot>

                  <legend id='9Htl3'><style id='9Htl3'><dir id='9Htl3'><q id='9Htl3'></q></dir></style></legend>

                  本文介绍了当我将 PyQt QProgressBar 与 Selenuim 一起使用时,它不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我构建了一个 PyQt5 GUI 来进行一些 Selenium 测试.一切都按预期工作,除了 PyQt 进度条.

                  I built a PyQt5 GUI to do some Selenium testing. Everything works as expected, except for the PyQt progress bar.

                  在下面的第一个示例中,我使用 Selenium 浏览器,最后,当浏览器关闭时,进度条会跳转到 100%.但是,Selenium 按预期工作.

                  In the first example below, where I use the Selenium browser, the progress bar just jumps to 100%, at the end, when the browser closes. But, the Selenium works as expected.

                  def test(self):
                          self.completed = 0
                          browser = webdriver.Firefox()
                          links = ['http://www.somesite.com/', 'http://www.somesite.com/page2',
                                   'http://www.somesite.com/page3']
                          for link in links:
                              browser.get(link)
                              self.completed += 100 / len(links)
                              time.sleep(2)
                              print(link)
                              self.progressBar.setValue(self.completed)
                          browser.close()
                  

                  但是,在下面的这个版本中,在注释掉 Selenium 浏览器的情况下,进度条按预期工作.

                  But, in this version below, with the Selenium browser commented out, the progress bar works as expected.

                  def test(self):
                          self.completed = 0
                          #browser = webdriver.Firefox()
                          links = ['http://www.somesite.com/', 'http://www.somesite.com/page2',
                                   'http://www.somesite.com/page3']
                          for link in links:
                              #browser.get(link)
                              self.completed += 100 / len(links)
                              time.sleep(2)
                              print(link)
                              self.progressBar.setValue(self.completed)
                          #browser.close()
                  

                  推荐答案

                  阻塞任务对执行 GUI 的事件循环不友好,因为它们阻止了 GUI 执行的正常任务,例如票证检查、重绘等. 从被处决.

                  The blocking tasks are not friendly with the event loop where the GUI is executed as they prevent the normal tasks that the GUI performs such as ticket checking, redrawing, etc. from being executed.

                  在这些情况下的解决方案是使用线程执行阻塞任务并使用信号发送信息.

                  The solution in these cases is to use thread to execute the blocking task and use the signals to send the information.

                  import sys
                  
                  from PyQt5 import QtCore, QtWidgets
                  
                  from selenium import webdriver
                  
                  class SeleniumWorker(QtCore.QObject):
                      progressChanged = QtCore.pyqtSignal(int)
                      def doWork(self):
                          progress = 0
                          browser = webdriver.Firefox()
                          links = ['http://www.somesite.com/',
                          'http://www.somesite.com/page2',
                          'http://www.somesite.com/page3']
                          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)
                          button = QtWidgets.QPushButton("Start")
                          lay.addWidget(progressBar)
                          lay.addWidget(button)
                          self.thread = QtCore.QThread()
                          self.worker = SeleniumWorker()
                          self.worker.moveToThread(self.thread)
                          self.thread.started.connect(self.worker.doWork)
                          button.clicked.connect(self.thread.start)
                          self.worker.progressChanged.connect(progressBar.setValue, QtCore.Qt.QueuedConnection)
                  
                  
                  if __name__ == '__main__':
                      app = QtWidgets.QApplication(sys.argv)
                      w = Widget()
                      w.show()
                      sys.exit(app.exec_())
                  

                  这篇关于当我将 PyQt QProgressBar 与 Selenuim 一起使用时,它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:参数 1 有意外的类型“NoneType"? 下一篇:PyQt4 到 PyQt5 ->mainFrame() 已弃用,需要修复才能

                  相关文章

                  最新文章

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

                  • <bdo id='sI23y'></bdo><ul id='sI23y'></ul>

                  <legend id='sI23y'><style id='sI23y'><dir id='sI23y'><q id='sI23y'></q></dir></style></legend>
                    1. <small id='sI23y'></small><noframes id='sI23y'>

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