Use a Progress Bar in a GUI

2019-06-12 04:37发布

I have a interface that launches FME scripts (FME is an Extract-Transform-Load software) and I would like to have a progress bar that informs the user on the progress of the script execution.

After going through some documentation and forums, I understand that a progress bar requires a value to run (ie values from a loop). The thing is, I don't really have values, I only have a script that runs and I would like to know how far it is.

So I have a signal: each time a button is clicked it emits a signal to change the value of the progress bar. But when I write ui.progressBar.setValue() i need a value and I don't know what this value should be.

I don't know if i'm using the right tool or if there is something easier/better.

1条回答
Viruses.
2楼-- · 2019-06-12 04:44

Here is a snippet I often use in my different scripts when they execute long operations (for example HTTP requests). This is just a basic spinning wheel that disapear after a few seconds. But you can also use signals & slots to hide it.

import math, sys
from PyQt4.QtCore import Qt, QTimer
from PyQt4.QtGui import *

class Overlay(QWidget):

    def __init__(self, parent = None):

        QWidget.__init__(self, parent)
        palette = QPalette(self.palette())
        palette.setColor(palette.Background, Qt.transparent)
        self.setPalette(palette)

    def paintEvent(self, event):

        painter = QPainter()
        painter.begin(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.fillRect(event.rect(), QBrush(QColor(255, 255, 255, 127)))
        painter.setPen(QPen(Qt.NoPen))

        for i in range(6):
            if (self.counter / 5) % 6 == i:
                painter.setBrush(QBrush(QColor(127 + (self.counter % 5)*32, 127, 127)))
            else:
                painter.setBrush(QBrush(QColor(127, 127, 127)))
            painter.drawEllipse(
                self.width()/2 + 30 * math.cos(2 * math.pi * i / 6.0) - 10,
                self.height()/2 + 30 * math.sin(2 * math.pi * i / 6.0) - 10,
                20, 20)

        painter.end()

    def showEvent(self, event):

        self.timer = self.startTimer(50)
        self.counter = 0

    def timerEvent(self, event):

        self.counter += 1
        self.update()
        if self.counter == 60:
            self.killTimer(self.timer)
            self.hide()


class MainWindow(QMainWindow):

    def __init__(self, parent = None):

        QMainWindow.__init__(self, parent)

        widget = QWidget(self)
        self.editor = QTextEdit()
        self.editor.setPlainText("0123456789"*100)
        layout = QGridLayout(widget)
        layout.addWidget(self.editor, 0, 0, 1, 3)
        button = QPushButton("Wait")
        layout.addWidget(button, 1, 1, 1, 1)

        self.setCentralWidget(widget)
        self.overlay = Overlay(self.centralWidget())
        self.overlay.hide()
        button.clicked.connect(self.overlay.show)

    def resizeEvent(self, event):

        self.overlay.resize(event.size())
        event.accept()


if __name__ == "__main__":

    app = QApplication(sys.argv)
    window = MainWindow()
    window.show()
    sys.exit(app.exec_())

As you are not providing any code, I can't adapt it to fit your needs.

Also, here is the source of the snippet. There are a lot of interesting snippets on this wiki: Python.org wiki

查看更多
登录 后发表回答