简体   繁体   English

如何使用pyqt4更新进度条

[英]How to update the progress bar using with pyqt4

Here it is sample program i want to update the my progress bar using with pyqt4.and i want to show th3 30% data saving and another 60% data processing.I am executing the my program it is aborting.Can any one please help me how to update the my progress bar.Thank you in advance.这是示例程序,我想使用 pyqt4 更新我的进度条。我想显示 th3 30% 的数据保存和另外 60% 的数据处理。我正在执行我的程序,它正在中止。任何人都可以帮助我如何更新我的进度条。提前谢谢。 Given below is my code:下面给出的是我的代码:

import sys
import time
from pyface.qt import QtGui, QtCore
global X,Y
X= 5
Y= 4

import threading
class SaveWorker(QtCore.QObject):
    progress_update = QtCore.Signal(int)
    def save_file(self):
        while True:
            MyCustomWidget().updateProgressBar()

class Dialog(QtGui.QDialog):
    def __init__(self, parent = None):
        super(Dialog, self).__init__(parent)
        self.setStyleSheet("QDialog {background-color:black; color:white }")
        self.label1 = QtGui.QLabel(
            text="Please Wait...",
            font=QtGui.QFont("Times", 20,weight=QtGui.QFont.Bold)
        )
        self.progress = QtGui.QProgressBar()
        self.box = QtGui.QVBoxLayout()
        self.label2 = QtGui.QLabel()
        vbox = QtGui.QVBoxLayout(self)
        vbox.addWidget(self.label1)
        vbox.addLayout(self.box)
        self.show_gif()
    def show_gif(self):
        self.progress = QtGui.QProgressBar()
        self.progress.setRange(0,100)

        self.box.addWidget(self.progress)
        self.show()
class MyCustomWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyCustomWidget, self).__init__(parent)
        self.worker = SaveWorker()
        self.gif_dialog = Dialog()
        self.worker.progress_update.connect(self.gif_dialog.show_gif)
        thread = threading.Thread(target=self.worker.save_file)
        thread.daemon = True
        thread.start()
        self.progressPer = 0
        fileList = []

        processes = []
        _dataSavingPer = 30.0/(X*Y)
        for i in range(X*Y):
            name =  'file'+str(i+1) + ".txt"
            fileList.append(name)
            self.progressPer += _dataSavingPer
            self.updateProgressBar(self.progressPer)
            #updating the progress bar
        _dataProcessPer = 60.0/(X*Y)
        for file in fileList:
            process = 'fileProcess'+str(i+1) + ".txt"
            processes.append(process)
            self.progressPer += _dataProcessPer
            self.updateProgressBar(self.progressPer)
            #Updating the progressPer
            #how can i update these two values in to progressbar
    def updateProgressBar(self,value):
            self.gif_dialog.progress.setValue(value)
if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyCustomWidget()
    sys.exit(app.exec_())

I do not understand what you tried when writing the following:我不明白您在编写以下内容时尝试了什么:

class SaveWorker(QtCore.QObject):
    progress_update = QtCore.Signal(int)
    def save_file(self):
        while True:
            MyCustomWidget().updateProgressBar()

updateProgressBar requires a value what value are you going through?, on the other hand when using MyCustomWidget() you are creating an object different from the one shown, and no MyCustomWidget object should be created in another thread. updateProgressBar需要一个值,您正在经历什么值?另一方面,当使用MyCustomWidget()您正在创建一个与所示对象不同的对象,并且不应在另一个线程中创建MyCustomWidget对象。

What you have to do is move the heavy task to the save_file method since it will be executed in another thread:您需要做的是将繁重的任务移至 save_file 方法,因为它将在另一个线程中执行:

import sys
import threading
from pyface.qt import QtGui, QtCore

X, Y = 5, 4

class SaveWorker(QtCore.QObject):
    progressChanged = QtCore.Signal(int)

    def save_file(self):
        fileList = []
        processes = []
        _dataSavingPer = 30.0/(X*Y)
        progress = 0
        for i in range(X*Y):
            name =  'file'+str(i+1) + ".txt"
            fileList.append(name)
            progress += _dataSavingPer
            self.progressChanged.emit(progress)
        _dataProcessPer = 60.0/(X*Y)
        for file in fileList:
            process = 'fileProcess'+str(i+1) + ".txt"
            processes.append(process)
            progress += _dataProcessPer
            self.progressChanged.emit(progress)

class Dialog(QtGui.QDialog):
    def __init__(self, parent = None):
        super(Dialog, self).__init__(parent)
        self.setStyleSheet("QDialog {background-color:black; color:white }")
        self.label1 = QtGui.QLabel(
            text="Please Wait...",
            font=QtGui.QFont("Times", 20,weight=QtGui.QFont.Bold)
        )
        self.progress = QtGui.QProgressBar()
        self.box = QtGui.QVBoxLayout()
        self.label2 = QtGui.QLabel()
        vbox = QtGui.QVBoxLayout(self)
        vbox.addWidget(self.label1)
        vbox.addLayout(self.box)
        self.show_gif()

    def show_gif(self):
        self.progress = QtGui.QProgressBar()
        self.progress.setRange(0,100)
        self.box.addWidget(self.progress)
        self.show()

class MyCustomWidget(QtGui.QWidget):
    def __init__(self, parent=None):
        super(MyCustomWidget, self).__init__(parent)
        self.worker = SaveWorker()
        self.gif_dialog = Dialog()
        self.worker.progressChanged.connect(self.gif_dialog.progress.setValue)
        thread = threading.Thread(target=self.worker.save_file)
        thread.daemon = True
        thread.start()

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = MyCustomWidget()
    sys.exit(app.exec_())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM