Python 将命令行结果重定向到tkinter GUI
我创建了一个在命令行上打印结果的程序。 (它是服务器,在命令行上打印日志。) 现在,我想在GUI上看到相同的结果 如何将命令行结果重定向到GUI? 请提供一个技巧,将控制台应用程序轻松转换为简单的GUIPython 将命令行结果重定向到tkinter GUI,python,user-interface,tkinter,Python,User Interface,Tkinter,我创建了一个在命令行上打印结果的程序。 (它是服务器,在命令行上打印日志。) 现在,我想在GUI上看到相同的结果 如何将命令行结果重定向到GUI? 请提供一个技巧,将控制台应用程序轻松转换为简单的GUI 请注意,它应该可以在Linux和Windows上运行。您可以创建一个脚本包装器,作为子进程运行命令行程序,然后将输出添加到类似文本小部件的内容中 from tkinter import * import subprocess as sub p = sub.Popen('./script',std
请注意,它应该可以在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()
脚本是您的程序所在的位置。显然,您可以用不同的颜色或类似的颜色打印错误。将stdout重定向到更新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!"
要在GUI中显示仍在运行的子进程的输出,同时在Python 2和Python 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线程中的队列获取输出
i、 例如,在后台线程->队列->主线程中更新GUI标签中调用
process.readline()
。相关(无轮询——在后台线程中使用event\u generate
的可移植性较差的解决方案)。很抱歉我的英语不好。实际上,我使用了另一种方式将命令提示符输出打印到新的自动化工具中。
请在下面找到这些步骤
1> 创建Bat文件并将其输出重定向到日志文件。
命令提示符命令:tasklist/svc
2> 使用Python3.x读取该文件。
`processedFile=open('D:\LOG\taskLog.txt','r')
3> 最后一步。
ttk.Label(Tab4,text=[ProcessFile.read()]).place(x=0,y=27)
**因此,请注意,我还没有将滚动条包含在这段代码中
发布截图:
启动事件循环的末尾缺少root.mainloop()。除此之外,它看起来应该可以工作。
.communicate()
等待脚本退出。如果OP希望在进程仍在运行时显示输出(正如“它是服务器,在命令行上打印日志”所暗示的那样),它将不起作用。它还会显示cmd当前目录,如C:\Users\Admin\Unknown吗\etc@KumarSaptam不对于linux中的python脚本,它只是返回由“\n”分隔的普通输出流和错误流:p=sub.Popen(['python3','script.py',stdout=sub.PIPE,stderr=sub.PIPErelated):如果调用os.write(1,b'not redirected'),它不会在文件描述符级别重定向sys.stdout
在test\u script.py
中,您将无法在GUI中看到它。请看,对于我来说,它在没有Queque
的情况下也可以工作,但我还将传入的信息附加到文本
小部件中,而不是在标签
中显示它。通过添加sui=subprocess.STARTUPINFO();sui.dwFlags |=subprocess.STARTF_USESHOWWINDOW
然后将startupinfo=sui
作为额外参数传递给subprocess.Popen
构造函数,我去掉了额外的shell输出提示。但仍然感谢您的回答,使我的脑细胞免于痛苦地死去^^