Python 3.5,pyqt5进度条gui在单独的窗口中

时间:2016-11-04 23:23:10

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

我是PyQt5的新手,也是Python的新手。我试图在Python 3.5中使用PyQt5创建一个图形用户界面,我点击一个按钮启动一个单独的窗口,其中进度条迭代到100,然后在迭代结束时关闭窗口以生成消息&# 34;它起作用了#34;。

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

我的进度条形码如下所示:

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_())

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:2)

这是我的最终代码。我试图在使用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_())

答案 1 :(得分:-1)

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

1)只应在主应用程序中再次调用Add QApplication.processEvents()一次

2)window.close()到forloop所以它会不断更新进度条

3)在forloop之后添加 private void openPicture() { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; try { InputStream stream = context.getContentResolver().openInputStream(uri); BitmapFactory.decodeStream(stream); int width = options.outWidth; int height = options.outHeight; Log.d("DEBUG", ""+ width); loadPicture(width, height); } catch(IOException e) { Log.e("FingerPainterView", e.toString()); } } private void loadPicture(int w, int h) { try { // attempt to load the uri provided, scale to fit our canvas InputStream stream = context.getContentResolver().openInputStream(uri); Bitmap bm = BitmapFactory.decodeStream(stream); bitmap = Bitmap.createScaledBitmap(bm, Math.max(w, h), Math.max(w, h), false); stream.close(); bm.recycle(); } catch(IOException e) { Log.e("FingerPainterView", e.toString()); } canvas = new Canvas(bitmap); } 以便关闭