24

我创建了一个在命令行上打印结果的程序。(它是服务器,它在命令行上打印日志。)

现在,我想看到与 GUI 相同的结果。

如何将命令行结果重定向到 GUI?

请提出一个将控制台应用程序轻松转换为简单 GUI 的技巧。

请注意,它应该适用于 Linux 和 Windows。

4

4 回答 4

17

您可以创建一个脚本包装器,将命令行程序作为子进程运行,然后将输出添加到文本小部件之类的东西中。

from tkinter import *
import subprocess as sub
p = sub.Popen('./script',stdout=sub.PIPE,stderr=sub.PIPE)
output, errors = p.communicate()

root = Tk()
text = Text(root)
text.pack()
text.insert(END, output)
root.mainloop()

其中 script 是您的程序。您显然可以用不同的颜色或类似的颜色打印错误。

于 2009-03-20T10:23:53.083 回答
15

要在 GUI仍在运行时显示子进程的输出,适用于 Python 2 和 3 的可移植 stdlib-only 解决方案必须使用后台线程:

#!/usr/bin/python
"""
- read output from a subprocess in a background thread
- show the output in the GUI
"""
import sys
from itertools import islice
from subprocess import Popen, PIPE
from textwrap import dedent
from threading import Thread

try:
    import Tkinter as tk
    from Queue import Queue, Empty
except ImportError:
    import tkinter as tk # Python 3
    from queue import Queue, Empty # Python 3

def iter_except(function, exception):
    """Works like builtin 2-argument `iter()`, but stops on `exception`."""
    try:
        while True:
            yield function()
    except exception:
        return

class DisplaySubprocessOutputDemo:
    def __init__(self, root):
        self.root = root

        # start dummy subprocess to generate some output
        self.process = Popen([sys.executable, "-u", "-c", dedent("""
            import itertools, time

            for i in itertools.count():
                print("%d.%d" % divmod(i, 10))
                time.sleep(0.1)
            """)], stdout=PIPE)

        # launch thread to read the subprocess output
        #   (put the subprocess output into the queue in a background thread,
        #    get output from the queue in the GUI thread.
        #    Output chain: process.readline -> queue -> label)
        q = Queue(maxsize=1024)  # limit output buffering (may stall subprocess)
        t = Thread(target=self.reader_thread, args=[q])
        t.daemon = True # close pipe if GUI process exits
        t.start()

        # show subprocess' stdout in GUI
        self.label = tk.Label(root, text="  ", font=(None, 200))
        self.label.pack(ipadx=4, padx=4, ipady=4, pady=4, fill='both')
        self.update(q) # start update loop

    def reader_thread(self, q):
        """Read subprocess output and put it into the queue."""
        try:
            with self.process.stdout as pipe:
                for line in iter(pipe.readline, b''):
                    q.put(line)
        finally:
            q.put(None)

    def update(self, q):
        """Update GUI with items from the queue."""
        for line in iter_except(q.get_nowait, Empty): # display all content
            if line is None:
                self.quit()
                return
            else:
                self.label['text'] = line # update GUI
                break # display no more than one line per 40 milliseconds
        self.root.after(40, self.update, q) # schedule next update

    def quit(self):
        self.process.kill() # exit subprocess if GUI is closed (zombie!)
        self.root.destroy()


root = tk.Tk()
app = DisplaySubprocessOutputDemo(root)
root.protocol("WM_DELETE_WINDOW", app.quit)
# center window
root.eval('tk::PlaceWindow %s center' % root.winfo_pathname(root.winfo_id()))
root.mainloop()

解决方案的本质是:

  • 将子进程输出放入后台线程的队列中
  • 从 GUI 线程中的队列中获取输出。

即,process.readline()在后台线程中调用-> 队列-> 在主线程中更新GUI 标签。相关kill-process.py(无轮询——event_generate在后台线程中使用的便携性较差的解决方案)。

于 2015-09-20T17:55:36.453 回答
6

将标准输出重定向到更新您的 gui 的 write() 方法是一种方法,并且可能是最快的方法 - 尽管运行子进程可能是一种更优雅的解决方案。

但是,只有在您真正确信它已启动并正常工作时才重定向 stderr!

实施示例(gui 文件和测试脚本):

test_gui.py:

from Tkinter import *
import sys
sys.path.append("/path/to/script/file/directory/")

class App(Frame):
    def run_script(self):
        sys.stdout = self
        ## sys.stderr = self
        try:
            del(sys.modules["test_script"])
        except:
            ## Yeah, it's a real ugly solution...
            pass
        import test_script
        test_script.HelloWorld()
        sys.stdout = sys.__stdout__
        ## sys.stderr = __stderr__

    def build_widgets(self):
        self.text1 = Text(self)
        self.text1.pack(side=TOP)
        self.button = Button(self)
        self.button["text"] = "Trigger script"
        self.button["command"] = self.run_script
        self.button.pack(side=TOP)

    def write(self, txt):
        self.text1.insert(INSERT, txt)

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.build_widgets()

root = Tk()
app = App(master = root)
app.mainloop()

test_script.py:

print "Hello world!"

def HelloWorld():
    print "HelloWorldFromDef!"
于 2009-03-20T12:01:36.700 回答
6

对不起,我的英语不好。实际上,我使用了一种不同的方式将命令提示符输出打印到我的新自动化工具中。请在下面找到这些步骤。

1> 创建一个 Bat 文件并将其输出重定向到一个 LOG 文件。命令提示符命令:tasklist /svc

2> 使用 Python 3.x 读取该文件。`processedFile = open('D:\LOG\taskLog.txt', 'r')

3> 压轴步骤。 ttk.Label(Tab4, text=[ProcessFile.read()]).place(x=0, y=27)

**因此请注意,我尚未在此代码中包含滚动条。

发布截图:

在此处输入图像描述

于 2018-04-09T22:40:18.050 回答