如何访问 GUI 输出? [英] How to access the GUI output?

查看:50
本文介绍了如何访问 GUI 输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个测试平台,它通过 python gui 运行多个测试并打印如下输出.

I'm developing one test bench which runs multiple tests via python gui and prints the output as below.

A Passed
B Passed
C Passed
D Passed
E Passed

只有当 A、B、C、D、E 都通过时,gui 的按钮才应该更改为通过".如果这些测试中的任何一个失败,它应该说失败.从屏幕上打印的 gui 访问此输出的方法是什么.

Button from gui should be changed to 'Passed' only when A,B,C,D,E all are Passed. If any of these tests fails, it should say failed. What is the way to access this output from gui which is printed on screen.

我的测试代码是:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
import sys, os, time
from PyQt4 import QtGui, QtCore
from progress.bar import Bar
import datetime 
import thread

class MyTestBench(QDialog, QtGui.QWidget):
    def __init__(self):
        super(QDialog, self).__init__()
        self.setWindowTitle("Implementation")

        self.progressbar = QtGui.QProgressBar()
        self.progressbar.setMinimum(0)
        self.progressbar.setMaximum(100)
        self.run_test_button = QtGui.QPushButton('Run Your Tests')
        self.run_test_button.clicked.connect(self.run_test_event)

    def run_test_event(self):
        thread.start_new_thread(self.run_the_test, ("Thread-1", 0.5))
        thread.start_new_thread(self.run_the_progress, ("Thread-2", 0.5))


    def run_the_test(self, tname, delay):        
        os.system("python nxptest.py my_testlist.txt")
        self.progressbar.setValue(100)
        if self.progressbar.value() == self.progressbar.maximum(): 
            time.sleep(3)
            self.run_test_button.setText('Run Your Tests')


    def run_the_progress(self, tname, delay):
        count = 0
        while count < 5:
            self.run_test_button.setText('Running.')
            time.sleep(0.5)
            self.run_test_button.setText('Running..')
            time.sleep(0.5)
            self.run_test_button.setText('Running...')
            value = self.progressbar.value() + 10
            self.progressbar.setValue(value)
            time.sleep(0.5)
            if self.progressbar.value() == self.progressbar.maximum():
                self.progressbar.reset()
            count = count + 1

app = QApplication(sys.argv)
dialog = MyTestBench()
dialog.setGeometry(100, 100, 200, 50)
dialog.show()
app.exec_()

我在这里面临的主要挑战是我是 gui 编程的新手,我不知道如何访问打印在屏幕上的输出.

The main challenge I'm facing here is I'm new to gui programming and I don't know how to access the output that is printed on screen.

推荐答案

如果您尝试获取程序的文本输出,则无法使用 os.system 运行该程序.正如该函数的文档所说:

If you're trying to get the text output of a program, you can't run that program using os.system. As the docs for that function say:

subprocess 模块为生成新进程和检索结果提供了更强大的工具;使用该模块比使用此功能更可取.请参阅subprocess 模块替换旧函数<subprocess 中的/a> 部分> 一些有用食谱的文档.

The subprocess module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in the subprocess documentation for some helpful recipes.

如果您点击这些链接,它们会告诉您如何做您想做的事.但基本上,它是这样的:

If you follow those links, they'll show how to do what you want. But basically, it's something like this:

output = subprocess.check_output(["python", "nxptest.py", "my_testlist.txt"])

如果您使用的是 2.6 或更早版本,则不会有 check_output;您可以阅读文档以了解如何在例如 communicate 之上自行构建它,或者您可以只安装 subprocess32 从 PyPI 向后移植并使用它.

If you're using 2.6 or earlier, you won't have check_output; you can read the docs to see how to build it yourself on top of, e.g., communicate, or you can just install the subprocess32 backport from PyPI and use that.

来自评论:

这有效,但我唯一担心的是在实际打印 A Passed B Passed 等之前打印的测试有很多结果.我正在寻找一种方法来获取字符串的这一部分而不是整个输出.

This works but my only concern is there are lot of results for the tests which are printed before it actually prints A Passed B Passed etc.. Im looking for a way to get just this part of string and not the whole output.

那是不可能的.你的程序怎么知道输出的哪一部分是字符串的这一部分",哪一部分是很多结果……之前打印过的"?

That isn't possible. How could your program have any idea which part of the output is "this part of the string" and which part is "a lot of results … which are printed before"?

如果您可以以某种方式编辑正在测试的程序——例如,让它们将它们的真实"输出打印到 stdout,而将它们的额外"输出打印到 stderr,或者提供一个命令行参数使它们跳过所有额外的东西——太好了.但假设您不能,则别无选择,只能过滤结果.

If you can edit the programs being tested in some way—e.g., make them print their "real" output to stdout, but their "extra" output to stderr, or provide a command-line argument that makes them skip all the extra stuff—that's great. But assuming you can't, there is no alternative but to filter the results.

但这看起来并不难.如果真实"输出的每一行都是 "X Passed""X Failed" 并且没有其他内容以 "X " 开头(其中X 是任何大写的 ASCII 字母),就是:

But this doesn't look very hard. If each line of "real" output is either "X Passed" or "X Failed" and nothing else starts with "X " (where X is any uppercase ASCII letter), that's just:

test_results = {}
for line in output.splitlines():
    if line[0] in string.ascii_uppercase and line[1] == ' ':
        test_results[line[0]] = line[2:]

现在,最后,你有:

{'A': 'Passed', 'B': 'Passed', 'C': 'Passed', 'D': 'Passed', 'E': 'Passed'}

如果你想验证所有 A-E 都被覆盖并且它们都通过了:

If you want to verify that all of A-E were covered and they all passed:

passed = (set(test_results) == set('ABCDE') and
          all(value == 'Passed' for value in test_results.values()))

当然,您可以构建更好的东西来显示哪些被跳过或没有通过或其他什么.但老实说,如果您想要更强大的功能,您可能应该使用现有的单元测试框架,而不是从头开始构建.

Of course you could build something nicer that shows which ones were skipped or didn't pass or whatever. But honestly, if you want something more powerful, you should probably be using an existing unit testing framework instead of building one from scratch anyway.

这篇关于如何访问 GUI 输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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