将命令行结果重定向到 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.