PySimpleGUIのボタン(ポップアップ)について

目次

PySimpleGUIのボタン(ポップアップ)の種類を調べてみました

PySimpleGUIのボタン(ポップアップ)を使用する上で調べたことを記載していきます。

PySimpleGUIのウィンドウだけでは表記が難しく、ボタン(ポップアップ)があれば良いな~という思いから、私なりに調べてみました。

実際にPySimpleGUIのポップアップを使用した例として下記のブログも記載しているので参考にどうぞ。

公式のページ例からも紹介していきます

まずは公式のリンクを貼りますので読んでみることをおススメします。

英語ですが、やはり日本語ページより情報が豊富です。

上記内から一部抜粋して下記へ記載します。

便利そうなポップアップが色々ありますね。

sg.popup('popup')  # Shows OK button
sg.popup_ok('popup_ok')  # Shows OK button
sg.popup_yes_no('popup_yes_no')  # Shows Yes and No buttons
sg.popup_cancel('popup_cancel')  # Shows Cancelled button
sg.popup_ok_cancel('popup_ok_cancel')  # Shows OK and Cancel buttons
sg.popup_error('popup_error')  # Shows red error button
sg.popup_timed('popup_timed')  # Automatically closes
sg.popup_auto_close('popup_auto_close')  # Same as PopupTimed

上記のポップアップのパラメータを下記へ記載します。

バックグラウンドカラーなどは他と差別化する上で重要な役割になりそうです。

popup(args=*<1 or N object>,
    title = None,
    button_color = None,
    background_color = None,
    text_color = None,
    button_type = 0,
    auto_close = False,
    auto_close_duration = None,
    custom_text = (None, None),
    non_blocking = False,
    icon = None,
    line_width = None,
    font = None,
    no_titlebar = False,
    grab_anywhere = False,
    keep_on_top = False,
    location = (None, None),
    any_key_closes = False,
    image = None,
    modal = True)

実践を交えて紹介します

では上記公式から実践を交えて紹介していきます。

簡単にですが下記コードを書いてみました。

import PySimpleGUI as sg

sg.theme('LightGrey1')

layout = [
    [sg.Text('popup-test')],
    [sg.Button('popup')],  # Shows OK button
    [sg.Button('popup_ok')],  # Shows OK button
    [sg.Button('popup_yes_no')],  # Shows Yes and No buttons
    [sg.Button('popup_cancel')],  # Shows Cancelled button
    [sg.Button('popup_ok_cancel')],  # Shows OK and Cancel buttons
    [sg.Button('popup_error')],  # Shows red error button
    [sg.Button('popup_timed')],  # Automatically closes
    [sg.Button('popup_auto_close')]  # Same as PopupTimed
]

window = sg.Window('', layout)

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'popup':
        sg.popup('popup')
        continue

    if event == 'popup_ok':
        sg.popup_ok('popup_ok')
        continue

    if event == 'popup_yes_no':
        sg.popup_yes_no('popup_yes_no')
        continue

    if event == 'popup_cancel':
        sg.popup_cancel('popup_cancel')
        continue

    if event == 'popup_ok_cancel':
        sg.popup_ok_cancel('popup_ok_cancel')
        continue

    if event == 'popup_error':
        sg.popup_error('popup_error')
        continue

    if event == 'popup_timed':
        sg.popup_timed('popup_timed')
        continue

    if event == 'popup_auto_close':
        sg.popup_auto_close('popup_auto_close')
        continue

window.close()

下記コードでポピュラーなポップアップが表示されました

sg.popup('popup')

ポップアップOK表示です。正直使いどころが分かってません(笑)

sg.popup_ok('popup_ok')

ポップアップの「yes」と「no」表示です。なかなか使用場面が多そうな感じします。

sg.popup_yes_no('popup_yes_no')

ポップアップのキャンセルボタンです。見た目分かりやすくいい感じです。

sg.popup_cancel('popup_cancel')

ポップアップの「OK」と「Cancel」ボタンです。こちらも使いどころ多そうです。

sg.popup_ok_cancel('popup_ok_cancel')

ポップアップエラー表示です。標準で赤表示なのでこれも使いどころ多そうですね。

sg.popup_error('popup_error')

ポップアップタイムです。どうもこちらは一定時間後にポップアップが自動で消えるようです「timed」と「auto_close」も一定時間後(2~3秒ぐらい)に閉じます。同じような動作なので使い分けが分かってません(笑)

sg.popup_timed('popup_timed')
sg.popup_auto_close('popup_auto_close')

他の言語を触ったことがないですが、Pythonではウィンドウ表示が簡単に表示出来ているように感じます。そういったところがPythonが人気あるところなのかもしれませんね。

ほかにも紹介できると良いのですが、面白そうな機能がありましたら紹介していきたいと思います。

ではでは。

最後に

「Power Automate Desktop」のインストールについて興味がある方は下記へアクセスください。

インストール手順を紹介しています。

Windows11への「Visual Studio Code」のインストールについて紹介しています。

Windows10と変わらなかったのですが参考になると嬉しいです。

  • URLをコピーしました!
目次