使用导入的模块中的变量报告进度到QProgressBar [英] Report progress to QProgressBar using variable from an imported module

查看:213
本文介绍了使用导入的模块中的变量报告进度到QProgressBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PyQT GUI应用程序 progress_bar.py 有一个进度条和一个外部模块 worker.py a process_files()函数,它使用文件列表执行一些例程,并使用 percent p>

我想要做的是使用 QProgressBar报告 worker.process_files 的当前进度.setValue()方法,但我不知道如何实现它(回调函数或某事?)



/ p>

progress_bar.py

  
从PyQt4导入QtGui
从工作进口process_files


类窗口(QtGui.QMainWindow):

def __init __(self):
super(Window,self).__ init __()
self.setGeometry(100,100,300,100)
self.progress = QtGui.QProgressBar(self)
self。 progress.setGeometry(100,50,150,20)
self.progress.setValue(0)
self.show()


app = QtGui.QApplication (sys.argv)
GUI = Window()
#使用.setValue(%)处理文件和报告进度
process_files()
sys.exit(app.exec_ )

worker.py

  def process_files():
file_list = ['file1','file2','file3']
counter = 0
文件in file_list:
#do_stuff_with_the_file
counter + = 1
percent = 100 * counter / len(file_list)
print percent


解决方案

使 process_files 函数生成函数产生一个值(进度值),并将其作为回调传递到更新进度条值的窗口类中的方法。我在您的函数中添加了 time.sleep 调用,以便您可以观察进度:

  import time 
from worker import process_files

class Window(QtGui.QMainWindow):
def __init __(self):
...

def observe_process(self,func = None):
try:
for fun in func():
self.progress.setValue(prog)
except TypeError:
print('callback function must be a generator function that yield integer values')
raise


app = QtGui.QApplication(sys.argv)
GUI = Window()
#使用.setValue(百分比)处理文件和报告进度
GUI.observe_process(process_files)
sys.exit(app.exec_ $ b

worker.py

  def process_files():
file_list = ['file1','file2','file3']
counter = 0
for file in file_list :
counter + = 1
percent = 100 * counter / len(file_list)
time.sleep(1)
yield percent






结果



处理完成后 file2




I have a PyQT GUI application progress_bar.pywith a single progressbar and an external module worker.py with a process_files() function which does some routine with a list of files and reports current progress using percent variable.

What I want to do is to report the current progress of the worker.process_files using QProgressBar.setValue() method, but I have no idea how to implement it (callback function or something?)

Here are my modules:

progress_bar.py

import sys
from PyQt4 import QtGui
from worker import process_files


class Window(QtGui.QMainWindow):

    def __init__(self):
        super(Window, self).__init__()
        self.setGeometry(100, 100, 300, 100)
        self.progress = QtGui.QProgressBar(self)
        self.progress.setGeometry(100, 50, 150, 20)
        self.progress.setValue(0)
        self.show()


app = QtGui.QApplication(sys.argv)
GUI = Window()
# process files and report progress using .setValue(percent)
process_files()
sys.exit(app.exec_())

worker.py

def process_files():
    file_list = ['file1', 'file2', 'file3']
    counter = 0
    for file in file_list:
        # do_stuff_with_the_file
        counter += 1
        percent = 100 * counter / len(file_list)
        print percent

解决方案

Make the process_files function a generator function that yields a value (the progress value) and pass it as a callback to a method in your Window class that updates the progress bar value. I have added a time.sleep call in your function so you can observe the progress:

import time
from worker import process_files

class Window(QtGui.QMainWindow):
    def __init__(self):
        ...

    def observe_process(self, func=None):
        try:
            for prog in func():
                self.progress.setValue(prog)
        except TypeError:
            print('callback function must be a generator function that yields integer values')
            raise


app = QtGui.QApplication(sys.argv)
GUI = Window()
# process files and report progress using .setValue(percent)
GUI.observe_process(process_files)
sys.exit(app.exec_())

worker.py

def process_files():
    file_list = ['file1', 'file2', 'file3']
    counter = 0
    for file in file_list:
        counter += 1
        percent = 100 * counter / len(file_list)
        time.sleep(1)
        yield percent


Result:

After processing file2

这篇关于使用导入的模块中的变量报告进度到QProgressBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆