我创建了一个在命令行上打印结果的程序.(它是服务器,它在命令行上打印日志.)
现在,我希望看到与GUI相同的结果.
如何将命令行结果重定向到GUI?
请提出一个技巧,以便轻松将控制台应用程序转换为简单的GUI.
请注意,它应该适用于Linux和Windows.
您可以创建一个脚本包装器,将命令行程序作为子进程运行,然后将输出添加到文本小部件之类的内容中.
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()
脚本是你的程序.显然,你可以用不同的颜色或类似的颜色打印错误.
要在GUI 仍在运行时在GUI中显示子进程的输出,在Python 2和3上都可以使用的便携式stdlib解决方案必须使用后台线程:
#!/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
在后台线程中使用的不太便携的解决方案).