1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[python]pysimpleguiのMultilineに追記していく方法

Posted at

はじめに

pysimpleguiのmultilineにログを出力したいのだが
毎回更新されてしまうので、過去の出力内容も保持しつつ出力する方法についてメモ。

Logger.py
import PySimpleGUI as sg

# レイアウト
layout = [
    [sg.Input("input", key="-INPUT-")],
    [sg.Multiline(size=(80, 20), key='-LOG-', autoscroll=True)],
    [sg.Button('Add Log'), sg.Button('Finish')]
]

# ウィンドウの作成
window = sg.Window('Logger', layout)

def addLog(logMessage):
    log_text = values['-LOG-']  # 現在のログテキストを取得
    new_log = logMessage  # 追加するログのテキスト
    if not log_text:
        updated_log = log_text + new_log  # 現在のログに新しいログを追加
    else:
        updated_log = log_text + "\n" + new_log  # 現在のログに新しいログを追加
    window['-LOG-'].update(updated_log)  # ログを更新

# メインイベントループ
while True:
    event, values = window.read()
    
    # ウィンドウが閉じられた場合や終了ボタンが押された場合、ループを終了する
    if event == sg.WINDOW_CLOSED or event == 'Finish':
        break
    
    # ログ追加ボタンが押された場合、テキストをログに追加する
    if event == 'Add Log':
        addLog(values['-INPUT-'])

# ウィンドウを閉じる
window.close()

結果

image.png

参考文献

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?