PyQt QProgressBar not working when I use it with Selenuim(当我将 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 一起使用时,它不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:当我将 PyQt QProgressBar 与 Selenuim 一起使用时,它不起作用
基础教程推荐
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 筛选NumPy数组 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01