
けんろう
PySimpleGUIでは、GUIアプリの配色を自由に変更できるtheme関数があります。これをうまく使えば、オリジナルなGUIアプリを製作できます。
Pythonライブラリである、PySimpleGUIは、GUIアプリを簡単に制作することができます。
theme関数を使えば、GUIアプリの配色を自由に設定できます。ただ、多くのテーマがあるため、どれを使用すればよいか悩むことがあります。
公式サイトでは、このテーマを実際のウィンドウに反映させて、見せてくれるテーマブラウザというサンプルコードを公開しています。
このテーマブラウザを使って、テーマ選定する方法を紹介します。
【無料オンライン】ゼロから始めるPythonプログラミング入門講座(週5開催) テックジム
テーマブラウザとは
テーマブラウザは、公式サイトのクックブックで紹介されているレシピのひとつです。
The Original PySimpleGUI Cookbook - PySimpleGUI Documentation
テーマを選択すると、実際のウィンドウの配色を変更して、表示してくれます。
テーマブラウザのサンプルコード
import PySimpleGUI as sg
"""
Allows you to "browse" through the Theme settings. Click on one and you'll see a
Popup window using the color scheme you chose. It's a simple little program that also demonstrates
how snappy a GUI can feel if you enable an element's events rather than waiting on a button click.
In this program, as soon as a listbox entry is clicked, the read returns.
"""
sg.theme('Dark Brown')
layout = [[sg.Text('Theme Browser')],
[sg.Text('Click a Theme color to see demo window')],
[sg.Listbox(values=sg.theme_list(), size=(20, 12), key='-LIST-', enable_events=True)],
[sg.Button('Exit')]]
window = sg.Window('Theme Browser', layout)
while True: # Event Loop
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Exit'):
break
sg.theme(values['-LIST-'][0])
sg.popup_get_text('This is {}'.format(values['-LIST-'][0]))
window.close()
実行すると、以下の画面が表示されます。
すると、以下の図のような画面が表示され、選択したテーマの配色をもつウィンドウが表示されます。


リストの中から、「BluePurpie」を選択してみます。


リストの中から、「Purpie」を選択してみます。


リストの中から、「DarkBlue3」を選択してみます。


テーマブラウザを使えば、自分のイメージ通りかを実際のウィンドウで確認できるので、とても便利です。
まとめ
今回は、Pythonライブラリである、PySimpleGUIで、テーマを実際のウィンドウで表示できるテーマブラウザを紹介しました。
リンク
コメント