我有一个Python程序,它执行一组操作并在STDOUT上打印响应。现在我正在编写一个GUI,它将调用已经存在的代码,我想在GUI中打印相同的内容而不是STDOUT。我将使用Text小部件来实现此目的。我不想修改执行任务的现有代码(此代码也被其他程序使用)。
有人可以指出我如何使用现有的任务定义并使用其STDOUT结果并将其插入文本小部件中吗?在主GUI程序中,我想调用此任务定义并将其结果打印到STDOUT。有没有办法使用这些信息?
答案 0 :(得分:15)
您可以通过将sys.stdout
替换为您自己的写入文本小部件的类文件对象来解决此问题。
例如:
import Tkinter as tk
import sys
class ExampleApp(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
toolbar = tk.Frame(self)
toolbar.pack(side="top", fill="x")
b1 = tk.Button(self, text="print to stdout", command=self.print_stdout)
b2 = tk.Button(self, text="print to stderr", command=self.print_stderr)
b1.pack(in_=toolbar, side="left")
b2.pack(in_=toolbar, side="left")
self.text = tk.Text(self, wrap="word")
self.text.pack(side="top", fill="both", expand=True)
self.text.tag_configure("stderr", foreground="#b22222")
sys.stdout = TextRedirector(self.text, "stdout")
sys.stderr = TextRedirector(self.text, "stderr")
def print_stdout(self):
'''Illustrate that using 'print' writes to stdout'''
print "this is stdout"
def print_stderr(self):
'''Illustrate that we can write directly to stderr'''
sys.stderr.write("this is stderr\n")
class TextRedirector(object):
def __init__(self, widget, tag="stdout"):
self.widget = widget
self.tag = tag
def write(self, str):
self.widget.configure(state="normal")
self.widget.insert("end", str, (self.tag,))
self.widget.configure(state="disabled")
app = ExampleApp()
app.mainloop()
答案 1 :(得分:3)
在python中,无论何时调用print('examplestring'),都要间接调用sys.stdout.write('examplestring') :
from tkinter import *
root=Tk()
textbox=Text(root)
textbox.pack()
button1=Button(root, text='output', command=lambda : print('printing to GUI'))
button1.pack()
方法1:在GUI上打印
def redirector(inputStr):
textbox.insert(INSERT, inputStr)
sys.stdout.write = redirector #whenever sys.stdout.write is called, redirector is called.
root.mainloop()
事实上,我们正在调用print - (callsfor) - > sys.stdout.write - (callsfor) - >重定向器
方法2:编写装饰器 - 在CLI和GUI上打印
def decorator(func):
def inner(inputStr):
try:
textbox.insert(INSERT, inputStr)
return func(inputStr)
except:
return func(inputStr)
return inner
sys.stdout.write=decorator(sys.stdout.write)
#print=decorator(print) #you can actually write this but not recommended
root.mainloop()
装饰器做的是它实际上将func sys.stdout.write分配给func inner
sys.stdout.write=inner
和func inner在回调实际的sys.stdout.write之前添加了一行额外的代码
这样可以更新旧的func sys.stdout.write以获得新功能。 您会注意到我使用了try-except,如果打印到文本框时出现任何错误,我至少会将sys.stdout.write的原始函数保留到CLI
方法3:Bryan Oakley的例子
...
sys.stdout = TextRedirector(self.text, "stdout")
...
class TextRedirector(object):
def __init__(self, widget, tag="stdout"):
self.widget = widget
self.tag = tag
def write(self, str):
self.widget.configure(state="normal")
self.widget.insert("end", str, (self.tag,))
self.widget.configure(state="disabled")
他所做的是他使用Method .write(str)将sys.stdout分配给Class TextRedirector
所以打电话 print('string')-calls for-> sys.stdout.write('string')-callsfor-> TextRedirector.write('string')
答案 2 :(得分:1)
您可以使用subprocess.Popen
调用CLI程序,获取它生成的标准输出,并在文本小部件中显示它。
(未经测试)的某些内容:
import subprocess
with subprocess.Popen(your_CLI_program, stdout=subprocess.PIPE) as cli
line = cli.stdout.readline()
#process the output of your_CLI_program
print (line)
请注意,这将阻止,直到CLI程序完成执行,冻结GUI。为了解决阻塞问题,您可以将此代码放在threading.Thread
中,并在等待线程完成时让GUI更新。
答案 3 :(得分:0)
事实上,我认为这个问题不仅限于tkinter
。可以应用任何框架,因为它实际上是在重定向sys.stdout
。
我创建了一个课程(RedirectStdMsg
)。
tl; dr
original = sys.stdout
sys.stdout = everything_you_like
...
sys.stdout = original # restore
import sys
from typing import TextIO
from typing import Callable
# import tkinter as tk
class RedirectStdMsg:
__slots__ = ('original', 'output_device',)
def __init__(self, sys_std: TextIO):
self.output_device = None
self.original = sys_std
def __call__(self, output_device=Callable[[str], None]):
self.output_device = output_device
return self
def __enter__(self):
if self.output_device is None:
raise AttributeError('output_device is empty')
self.start(self.output_device)
def __exit__(self, exc_type, exc_val, exc_tb):
if exc_val:
self.write(str(exc_val))
self.stop()
def start(self, output_device):
self.output_device = output_device
std_name = self.original.name.translate(str.maketrans({'<': '', '>': ''}))
exec(f'sys.{std_name} = self') # just like: ``sys.stderr = self``
def stop(self):
std_name = self.original.name.translate(str.maketrans({'<': '', '>': ''}))
exec(f'sys.{std_name} = self.original')
self.output_device = None
def write(self, message: str):
""" When sys.{stderr, stdout ...}.write is called, it will redirected here"""
if self.output_device is None:
self.original.write(message)
self.original.flush()
return
self.output_device(message)
由@Bryan Oakley修改
class ExampleApp(tk.Tk):
def __init__(self, **options):
tk.Tk.__init__(self)
toolbar = tk.Frame(self)
toolbar.pack(side="top", fill="x")
b1 = tk.Button(self, text="print to stdout", command=self.print_stdout)
b2 = tk.Button(self, text="print to stderr", command=self.print_stderr)
b1.pack(in_=toolbar, side="left")
b2.pack(in_=toolbar, side="left")
self.text = tk.Text(self, wrap="word")
self.text.pack(side="top", fill="both", expand=True)
self.text.tag_configure("stderr", foreground="#b22222")
self.re_stdout = options.get('stdout')
self.re_stderr = options.get('stderr')
if self.re_stderr or self.re_stderr:
tk.Button(self, text='Start redirect', command=self.start_redirect).pack(in_=toolbar, side="left")
tk.Button(self, text='Stop redirect', command=self.stop_redirect).pack(in_=toolbar, side="left")
def start_redirect(self):
self.re_stdout.start(TextRedirector(self.text, "stdout").write) if self.re_stdout else ...
self.re_stderr.start(TextRedirector(self.text, "stderr").write) if self.re_stderr else ...
def stop_redirect(self):
self.re_stdout.stop() if self.re_stdout else ...
self.re_stderr.stop() if self.re_stderr else ...
@staticmethod
def print_stdout():
"""Illustrate that using 'print' writes to stdout"""
print("this is stdout")
@staticmethod
def print_stderr():
"""Illustrate that we can write directly to stderr"""
sys.stderr.write("this is stderr\n")
class TextRedirector(object):
def __init__(self, widget, tag="stdout"):
self.widget = widget
self.tag = tag
def write(self, msg):
self.widget.configure(state="normal")
self.widget.insert("end", msg, (self.tag,))
self.widget.configure(state="disabled")
def test_tk_without_stop_btn():
app = ExampleApp()
with RedirectStdMsg(sys.stdout)(TextRedirector(app.text, "stdout").write), \
RedirectStdMsg(sys.stderr)(TextRedirector(app.text, "stderr").write):
app.mainloop()
def test_tk_have_stop_btn():
director_out = RedirectStdMsg(sys.stdout)
director_err = RedirectStdMsg(sys.stderr)
app = ExampleApp(stdout=director_out, stderr=director_err)
app.mainloop()
def test_to_file():
# stdout test
with open('temp.stdout.log', 'w') as file_obj:
with RedirectStdMsg(sys.stdout)(file_obj.write):
print('stdout to file')
print('stdout to console')
# stderr test
with open('temp.stderr.log', 'w') as file_obj:
with RedirectStdMsg(sys.stderr)(file_obj.write):
sys.stderr.write('stderr to file')
sys.stderr.write('stderr to console')
# another way
cs_stdout = RedirectStdMsg(sys.stdout)
cs_stdout.start(open('temp.stdout.log', 'a').write)
print('stdout to file 2')
...
cs_stdout.stop()
print('stdout to console 2')
if __name__ == '__main__':
test_to_file()
test_tk_without_stop_btn()
test_tk_have_stop_btn()
答案 4 :(得分:-1)
通常打印到stdout的函数应该将文本放入文本小部件中。