【问题标题】:Python 3.5, pyqt5 progress bar gui in seperate windowPython 3.5,单独窗口中的 pyqt5 进度条 gui
【发布时间】:2016-11-04 23:23:09
【问题描述】:

我是 PyQt5 的新手,对 Python 还是很陌生。我正在尝试在 Python 3.5 中使用 PyQt5 创建一个图形用户界面,在其中单击一个按钮以启动一个单独的窗口,其中进度条迭代到 100,然后在迭代结束时关闭窗口以生成一条消息“它工作”。

问题是进度条已创建但没有更新,并且在到达结束后它不显示它工作的消息。当我尝试调试时,它完全崩溃,没有任何警告说明原因。不知道怎么调试代码

我的进度条码如下图:

from PyQt5 import QtCore, QtWidgets
import sys

class Ui_Form(object):
def setupUi(self, Form):
    Form.setObjectName("Form")
    Form.resize(1075, 84)
    self.progressBar = QtWidgets.QProgressBar(Form)
    self.progressBar.setGeometry(QtCore.QRect(30, 30, 1000, 35))
    sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
    sizePolicy.setHorizontalStretch(0)
    sizePolicy.setVerticalStretch(0)
    sizePolicy.setHeightForWidth(self.progressBar.sizePolicy().hasHeightForWidth())
    self.progressBar.setSizePolicy(sizePolicy)
    self.progressBar.setMinimumSize(QtCore.QSize(1000, 35))
    self.progressBar.setMaximumSize(QtCore.QSize(1000, 35))
    self.progressBar.setProperty("value", 0)
    self.progressBar.setObjectName("progressBar")

    self.retranslateUi(Form)
    QtCore.QMetaObject.connectSlotsByName(Form)

def setValue(self, val):
    self.progressBar.setProperty("value", val)

def retranslateUi(self, Form):
    _translate = QtCore.QCoreApplication.translate
    Form.setWindowTitle(_translate("Form", "Progress bar"))

下面给出主程序

from PyQt5.QtWidgets import QApplication, QDialog, QWidget, QPushButton, QMessageBox
import ProgressBar
import sys

class App(QWidget):
def __init__(self):
    super().__init__()
    self.title = 'PyQt5 button - pythonspot.com'
    self.left = 200
    self.top = 200
    self.width = 320
    self.height = 200
    self.initUI()

def initUI(self):
    self.setWindowTitle(self.title)
    self.setGeometry(self.left, self.top, self.width, self.height)

    button = QPushButton('PyQt5 button', self)
    button.setToolTip('This is an example button')
    button.move(100, 70)
    button.clicked.connect(self.on_click)

    self.show()

def on_click(self):
    print('PyQt5 button click')

    app1 = QApplication(sys.argv)
    window = QDialog()
    ui = ProgressBar.Ui_Form()
    ui.setupUi(window)
    window.show()

    for i in range(0, 100):
        ui.setValue(((i + 1) / 100) * 100)

    app1.quit()

    QMessageBox.information(self, "Message", "Data Loaded")

if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())

任何帮助将不胜感激。

【问题讨论】:

    标签: python-3.x user-interface progress-bar pyqt5


    【解决方案1】:

    这是我的最终代码。我试图在使用 pyqt 设计器时包含一些好的做法,而不是直接编辑创建的文件,而是从另一个文件运行它并调用它。当我想按照建议进行更改时,这使事情变得更加容易。我还在代码中加入了 time.sleep(0.1) 来减慢它的速度,这样你就可以看到它正在工作。希望对您有所帮助。

    ProgressBar_ui.py - 由 ProgressBar.ui 在 python 中转换生成的文件

    # -*- coding: utf-8 -*-
    
    # Form implementation generated from reading ui file 'Base.ui'
    #
    # Created by: PyQt5 UI code generator 5.6
    #
    # WARNING! All changes made in this file will be lost!
    
    from PyQt5 import QtCore, QtGui, QtWidgets
    
    class Ui_Form(object):
        def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(1075, 84)
        self.progressBar = QtWidgets.QProgressBar(Form)
        self.progressBar.setGeometry(QtCore.QRect(30, 30, 1000, 35))
        sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Fixed, QtWidgets.QSizePolicy.Fixed)
        sizePolicy.setHorizontalStretch(0)
        sizePolicy.setVerticalStretch(0)
        sizePolicy.setHeightForWidth(self.progressBar.sizePolicy().hasHeightForWidth())
        self.progressBar.setSizePolicy(sizePolicy)
        self.progressBar.setMinimumSize(QtCore.QSize(1000, 35))
        self.progressBar.setMaximumSize(QtCore.QSize(1000, 35))
        self.progressBar.setProperty("value", 0)
        self.progressBar.setObjectName("progressBar")
    
        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)
    
    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Progress bar"))
    

    ProgressBar.py - 调用 ProgrssBar.ui

    from PyQt5 import QtCore, QtGui, QtWidgets
    import sys
    
    from ProgressBar_ui import Ui_Form
    
    class ProgressBar(QtWidgets.QDialog, Ui_Form):
        def __init__(self, desc = None, parent=None):
            super(ProgressBar, self).__init__(parent)
            self.setupUi(self)
            self.show()
    
            if desc != None:
                self.setDescription(desc)
    
        def setValue(self, val): # Sets value
            self.progressBar.setProperty("value", val)
    
        def setDescription(self, desc): # Sets Pbar window title
            self.setWindowTitle(desc)
    
    def main():
        app = QtWidgets.QApplication(sys.argv)      # A new instance of QApplication
        form = ProgressBar('pbar')                        # We set the form to be our MainWindow (design)
        app.exec_()                                 # and execute the app
    
    if __name__ == '__main__':                      # if we're running file directly and not importing it
        main()                                      # run the main function
    

    Main_program.py - 从这里运行

    from PyQt5.QtWidgets import QApplication, QWidget, QPushButton, QMessageBox
    from ProgressBar import ProgressBar
    import sys, time
    
    class App(QWidget):
        def __init__(self):
            super().__init__()
            self.title = 'PyQt5 button - pythonspot.com'
            self.left = 200
            self.top = 200
            self.width = 320
            self.height = 200
            self.initUI()
    
        def initUI(self):
            self.setWindowTitle(self.title)
            self.setGeometry(self.left, self.top, self.width, self.height)
    
            button = QPushButton('PyQt5 button', self)
            button.setToolTip('This is an example button')
            button.move(100, 70)
            button.clicked.connect(self.on_click)
    
            self.show()
    
        def on_click(self):
            pb = ProgressBar()
    
            for i in range(0, 100):
                time.sleep(0.05)
                pb.setValue(((i + 1) / 100) * 100)
                QApplication.processEvents()
    
            pb.close()
    
            QMessageBox.information(self, "Message", "Data Loaded")
    
    if __name__ == '__main__':
        app = QApplication(sys.argv)
        ex = App()
        sys.exit(app.exec_())
    

    【讨论】:

      【解决方案2】:

      最终找到了解决方案。 3个问题:

      1) 只应在主应用程序中调用QApplication 一次,而不应再次调用

      2) Add QApplication.processEvents() 到 forloop,因此它会不断更新进度条

      3) 在 forloop 之后添加 window.close() 使其关闭

      【讨论】:

        猜你喜欢
        • 2023-03-26
        • 2017-03-07
        • 1970-01-01
        • 1970-01-01
        • 2021-10-10
        • 1970-01-01
        • 2023-03-02
        • 1970-01-01
        • 2019-05-15
        相关资源
        最近更新 更多