我的目标是向我的Tkinter GUI显示另一个程序(其代码不是我的)的日志。在我的GUI接受了输入之后,我已经使用子进程运行了另一个程序,但是它的输出显示在命令提示符中。
如何从另一个程序(在另一个文件夹中)访问getLogger并将其重定向到我的Text小部件?任何澄清我的线索或提示都将是一个很大的帮助!
这是一些片段。 ResultsInterface是来自gui接受输入的顶层窗口,在按下主gui的Run按钮后调用。我打算调用子进程(['python','program.py'] +输入)并在文本小部件上显示它。
class IODirector(object):
def __init__(self, text_area):
self.text_area = text_area
class StdoutDirector(IODirector):
def write(self, msg):
self.text_area.insert(END, msg)
def flush(self):
pass
class ResultsInterface():
def __init__(self, master, word = ""):
self.master = master
self.word = word
self.frame = Frame(self.master)
self.master.title("Results")
self.text_area = Text(master, borderwidth = 3, width = 80, height = 20, font = "Consolas 11", relief="sunken")
self.text_area.grid(row = 4, ipadx = 5, ipady = 5, padx = 10, pady = 10, sticky ="we")
self.do_run
self.frame.grid()
def do_run(self):
t = Thread(target=run_thug)
sys.stdout = StdoutDirector(self.text_area)
# configure the nameless "root" logger to also write
# to the redirected sys.stdout
#Just saw this snippet in other's answer, how do I show logs only to the Text text_area widget
logger = logging.getLogger("Program")
console = logging.StreamHandler(stream=sys.stdout)
logger.addHandler(console)
#t.start() #want it to run with the log appearing on my text_area
def run_thug(self):
input_args = "/path/to/program.py" + " " + self.word
cmdr = ['python']+(input_args.split(' '))
self.process = Popen((cmdr), stdout=PIPE)