LoginSignup
0
0

[python]pysimpleguiで他のpyファイルからmultilineにログ出力

Posted at

はじめに

他の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()

参考文献

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0