実際、この問題はに限定されていないと思います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)
テスト
tkクラスをテストする
@BryanOakleyから変更
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()
これはtest_tk_have_stop_btn():