PySimpleGui 中使用 logging 输出到 Outp

2023-05-06  本文已影响0人  LeslieLiang
from logging import basicConfig, INFO, info, Handler, getLogger
basicConfig(
    level=INFO,
    format='[%(asctime)s][%(levelname)s]: %(message)s',
    datefmt='%H:%M:%S',
)


class GuiLogger(Handler):
    def __init__(self, document) -> None:
        Handler.__init__(self)
        self.document = document

    def emit(self, record) -> None:
        self.document.update(value=self.document.get() + '\n' + self.format(record))

在 Window 创建完成后添加日志处理器

window = Window(
    'Test 1.0',
    self.layout,
)
getLogger().addHandler(GuiLogger(document=self.window['output']))

while True:
    event, values = self.window.read()
    if event == 'Exit' or event == WIN_CLOSED:
        break
    info('...')
上一篇下一篇

猜你喜欢

热点阅读