はじめに
他のpyファイルからUIのMultilineにログ出力する方法をメモ。
func.py
import logging
# ログの設定
logging.basicConfig(filename='log_file.txt', level=logging.DEBUG)
# ログの出力
logging.debug('This is a debug message')
logging.info('This is an info message')
logging.warning('This is a warning message')
GUI.py
import PySimpleGUI as sg
# ログファイルを読み取って内容を取得
with open('log_file.txt', 'r') as file:
log_content = file.read()
# GUIのレイアウト
layout = [
[sg.Multiline(log_content, size=(80, 20), key='-LOG-')],
[sg.Button('Refresh')]
]
# ウィンドウの作成
window = sg.Window('Log Viewer', layout)
# イベントループ
while True:
event, values = window.read()
if event == sg.WINDOW_CLOSED:
break
elif event == 'Refresh':
# ログファイルを再読み込みして内容を更新
with open('log_file.txt', 'r') as file:
log_content = file.read()
window['-LOG-'].update(log_content)
window.close()