2016-11-04 140 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_())