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.

PySimpleGUIのラジオボタンを保守性良く記述

Posted at

PySimpleGUIを使うと簡単にGUIアプリを作ることができて便利です。
ただ、その中でも、ラジオボタンは直接記述すると煩雑で保守性が悪くなってしまいます。
そこで、リストと辞書を活用して保守性の良い記述を考えてみました。

import PySimpleGUI as sg

rdco = {    #ラジオボタン共通設定(辞書)
    'font':('meiryo', 16),
    'enable_events':True
}
l_rd = [    #ラジオボタン設定 [group_id, デフォルト選択key, {共通オプション}, [[text, key]]]
    ['rtype', '-RD_RT_NORM-', rdco, [
            ['白米', '-RD_RT_NORM-'],
            ['雑穀米', '-RD_RT_ZAKKOU-']
        ]
    ],
    ['rsize', '-RD_RS1-', rdco, [
            ['小盛', '-RD_RS0-'],
            ['普通', '-RD_RS1-'],
            ['大盛', '-RD_RS2-']
        ]
    ]
]
l_rdFr = [  #ラジオボタンフレーム設定 [group_id, title]
    ['rtype', 'ごはんの種類'],
    ['rsize', 'ごはんの量']
]
RD = {gid : [sg.Radio(n, key=k, group_id=gid, default=(df==k), **op) for n, k in rs]
 for gid, df, op, rs in l_rd}

layout =[[sg.Frame(gn, [RD[gid]])] for gid, gn in l_rdFr]
window = sg.Window('radio buttons', layout)

while True:
    event, values = window.read()
    print(event, values)
    if event in (sg.WIN_CLOSED, 'Exit'):
        break
window.close()

PySimpleGUI_Radio.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?