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('...')
网友评论