2016-11-04 145 views
-1

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

问题是进度条被创建但不更新,到达结束后它不显示它的工作消息。当我尝试调试时,它完全崩溃,没有警告为什么。我不知道该怎么调试代码

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

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

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

1)如果仅在主应用程序调用的QApplication一旦不再 2))添加QApplication.processEvents(在for循环,因此更新进度不断 3)添加window.close()的后forloop所以它关闭

+0

你能显示代码吗? – Rahul

1

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

ProgressBar_ui.py - 从ProgressBar.ui

# -*- 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在python通过转换生成的文件 - 呼吁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_())