5. Radio Button
今回は「Radio Button」を中心に使い方などを解説していきます。
だだし作業手順などはこれまでの講座を参考にして下さい。
Python で GUI アプリケーション
また、今回利用する素材などは、次の zip ファイルに纏めてあります。
なお、アイコンなどは「ICOOON MONO」から利用させて頂きました。
Radio Button とは?
Radio Button は複数の選択から1つを選択する事が可能なボタンの事を言います。

しかしそのためにはグルーピングを行う必要があり、そのために「Group Box」を用います。

従って、Radio Button を設置するためには、先ず Group Box の配置から行う必要があります。
この時、Group Box 内に配置された Radio Button は自動的に1つしか選択できなくなります。
また、Group Box 内を右クリックする事により、レイアウトを適用する事も可能です。
また、Group Box にはラベルが付属していますので、何に対する選択なのかを記述しておくのが良いでしょう。(ここでは「フルーツ」としています)

Radio Button のグループが作成できた場合は、1つの Radio Button だけをチェックしておく必要があります。

この例では、リンゴの Radio Button をチェックしてあります。
ダイアログ
Radio Button の動作を確認するために、次のようなダイアログを作成しました。

オブジェクトは以下のような構造です。(画像は QLabel で作成しています)

シグナル/スロットは次の様に設定しています。

プロジェクト
プロジェクトは次のとおりです。

ソース
ソースは次のとおりです。 今回のポイントはバナナと桃の画像を非表示にしている事です。
#!/usr/bin/env python # This Python file uses the following encoding: utf-8 import sys from PySide6.QtWidgets import QApplication from PySide6.QtUiTools import loadUiType if __name__ == "__main__": # アプリケーションの開始 app = QApplication(sys.argv) # ダイアログファイルを読み込み専用でオープンし、ダイアログデータの読み込み generated_class, base_class = loadUiType("dialog.ui") window = base_class() form = generated_class() form.setupUi(window) #バナナと桃の画像の非表示 form.my_bananaLabel.hide() form.my_peachLabel.hide() # ダイアログの表示 window.show() # イベントループの実行 sys.exit(app.exec())
実行



その他
Radio Button に関する詳しい資料。QGroupBox, QRadioButton
次回



[…] 5. Radio Button […]