2016-11-04 7 views
-1

私はPyQt5が初めてで、Pythonにはかなり新しいです。 Python 3.5でPyqt5を使ってグラフィカルユーザーインターフェイスを作成しようとしています。ここで、ボタンをクリックしてプログレスバーを最大100回反復し、メッセージを生成するために反復の最後にウィンドウを閉じます。働いた "。Python 3.5、pyqt5 progress bar 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)のみを連続的 3)(window.closeを追加しませ再び 2)メインアプリケーションで一度はQApplicationを呼び出すことがプログレスバーを更新しますので、forloopに)(QApplication.processEventsを追加する必要があります)した後forloopので終了する

+0

コードを表示できますか? – Rahul

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 - ここ

から実行 - Main_program.py
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 

ProgrssBar.ui

を呼び出します。
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_()) 
関連する問題