将命令行结果重定向到 tkinter GUI [英] Redirect command line results to a tkinter GUI

查看:62
本文介绍了将命令行结果重定向到 tkinter GUI的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created a program that prints results on command line. (It is server and it prints log on command line.)

现在,我想在 GUI 上看到相同的结果.

Now, I want to see the same result to GUI .

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

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

Please, suggest a trick to easily transform console application to simple GUI.

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

Note that it should work on Linux and Windows.

推荐答案

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

You could create a script wrapper that runs your command line program as a sub process, then add the output to something like a text widget.

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()

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

where script is your program. You can obviously print the errors in a different colour, or something like that.

这篇关于将命令行结果重定向到 tkinter GUI的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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