image-analyzer-lab/examples/example_gui_pyside6.py
2025-09-12 08:48:34 +09:00

62 lines
2.0 KiB
Python

import sys
from PySide6.QtWidgets import (
QApplication, QWidget, QLabel, QLineEdit, QCheckBox, QListWidget,
QPushButton, QVBoxLayout, QHBoxLayout, QMessageBox
)
class MainWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("PySide6 GUI サンプル")
self.resize(500, 300)
# --- 左側ウィジェット ---
label = QLabel("名前を入力してください:")
self.entry = QLineEdit()
self.chk = QCheckBox("同意します")
left_layout = QVBoxLayout()
left_layout.addWidget(label)
left_layout.addWidget(self.entry)
left_layout.addWidget(self.chk)
left_layout.addStretch()
# --- 右側ウィジェット ---
self.listbox = QListWidget()
for item in ["りんご", "バナナ", "みかん", "ぶどう"]:
self.listbox.addItem(item)
btn_exec = QPushButton("実行")
btn_exec.clicked.connect(self.on_exec)
btn_quit = QPushButton("終了")
btn_quit.clicked.connect(self.close)
right_layout = QVBoxLayout()
right_layout.addWidget(self.listbox)
right_layout.addWidget(btn_exec)
right_layout.addWidget(btn_quit)
right_layout.addStretch()
# --- メインレイアウト(横並び) ---
main_layout = QHBoxLayout()
main_layout.addLayout(left_layout, 1)
main_layout.addLayout(right_layout, 1)
self.setLayout(main_layout)
def on_exec(self):
name = self.entry.text()
checked = "はい" if self.chk.isChecked() else "いいえ"
item = self.listbox.currentItem().text() if self.listbox.currentItem() else "未選択"
msg = f"こんにちは {name} さん\nチェック: {checked}\n選択: {item}"
QMessageBox.information(self, "挨拶", msg)
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())