【问题标题】:How to create activity indicator using pyQt4 designer for python如何使用 pyQt4 设计器为 python 创建活动指示器
【发布时间】:2016-01-24 09:59:19
【问题描述】:

我正在学习使用 pyQt4 的 GUI python。我在另一个文件 python 中有函数A。我想在我从文件.ui(设计师pyQt4的输出)中提取的GUI文件python中运行。如何创建在函数A 运行时处于活动状态的活动指示器?我可以在不知道我的函数A 运行多少时间的情况下使用进度条(在 pyQt4 设计器中)吗?

谢谢。

这是在 GUI .py 中调用A 的函数:

def RunFunction():
    import Kdtree
    _dir = kdTreeOk.getNeighbor(float(radius)) #function 'A'
    file = file_open('Summary.txt',_dir) # ignore, just file to save result of `A`
    with file:
         textOutput=file.read()
         ui.result.setPlainText(textOutput)

#### button to run RunFunction in file GUI .py

ui._run.clicked.connect(RunFunction)

【问题讨论】:

  • A 是否以任何方式与 GUI 交互? (它会调用 PyQt 函数吗?)
  • 我在文件 GUI .py 中创建函数来调用A。然后我连接按钮以使用该功能运行A。我将编写调用A的函数。

标签: python-2.7 pyqt4


【解决方案1】:

QProgressDialog 就是为此目的而创建的,通常通过QThread 调用。这是一个(混乱的)基本示例,展示了它是如何工作的(没有任何线程)。如果您从另一个窗口调用此对话框,只需将parent 设置为调用窗口,您就可以通过调用self.parent.some_variable 来读取此对话框中的属性。

EDITED 正常工作;)。

from PyQt4 import QtCore, QtGui
from time import sleep
import sys

class ProgressBarWidget(QtGui.QProgressDialog):

    def __init__(self, parent=None, app=None):
        super(ProgressBarWidget, self).__init__(parent)

        self.app=app
        self._allow_close = True

        layout = QtGui.QVBoxLayout(self)

        # Create a progress bar and a button and add them to the main layout
        self.progressBar = QtGui.QProgressBar(self)
        self.progressBar.setRange(0,100)
        layout.addWidget(self.progressBar)
        self.button = QtGui.QPushButton("Start", self)
        layout.addWidget(self.button)

        self.button.clicked.connect(self.onStart)

        self.upload_count = 10


    def onStart(self):
        self.progressBar.setValue(0)
        self.button.setText("Uploading...")
        self.run()

    def makeProgress(self, current_num, total_num, message = ''):
        if total_num == current_num:
            self.onFinished()

        elif current_num == 0:
            self.progressBar.setValue(0)
        else:
            multiplier = int(float(float(100) / float(total_num)))
            c_times_m = current_num * multiplier
            for i in xrange(c_times_m - int(self.progressBar.value())):
                new_val = int(self.progressBar.value()) + 1
                self.progressBar.setValue(new_val)
                sleep(.01)


    def onFinished(self):
        # progress complete
        self.progressBar.setRange(0,100)
        for i in xrange(int(self.progressBar.value()),101):
            self.progressBar.setValue(i)
        self.button.setEnabled(True)
        self.button.setText('Exit')
        self.button.clicked.disconnect(self.onStart)
        self.button.clicked.connect(self.close)

    def run(self):
        self._allow_close = False
        self.button.setDisabled(True)

        total = self.upload_count * 2

        progress_meter = 0

        downloaded = []
        tests_to_upload = 10
        for each in xrange(tests_to_upload):
            sleep(0.15)
            progress_meter += 1
            self.makeProgress(progress_meter,total)

            sleep(0.2)
            progress_meter += 1

            self.makeProgress(progress_meter, total)

if __name__ == "__main__":
    app = QtGui.QApplication(sys.argv)
    window = ProgressBarWidget(app=app)
    window.resize(640, 480)
    window.show()
    sys.exit(app.exec_())

【讨论】:

  • 对不起,你能给我一个在我的例子中使用这个类的例子吗?我是空白的:'(
猜你喜欢
  • 2018-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多