Python PyQt よく使うコンポーネント
PyQt は Python 言語の GUI プログラミングツールキットであり、Qt アプリケーションフレームワークの Python バインディングです。
Qt はクロスプラットフォームの C++ グラフィカルユーザーインターフェースアプリケーション開発フレームワークであり、GUI プログラムの開発に広く使用されています。
PyQt は、ユーザーインターフェースを構築するための豊富なコンポーネント(Widgets)を提供します。これらのコンポーネントは、GUI アプリケーションを構築するための基本モジュールです。
基本ウィンドウコンポーネント
QMainWindow
QMainWindow はメインウィンドウクラスであり、標準的なアプリケーションフレームワークを提供します:
サンプル
from PyQt5.QtWidgets import QMainWindow, QApplication
app = QApplication([])
window = QMainWindow()
window.setWindowTitle('メインウィンドウの例')
window.setGeometry(100, 100, 800, 600) # x, y, width, height
window.show()
app.exec_()
app = QApplication([])
window = QMainWindow()
window.setWindowTitle('メインウィンドウの例')
window.setGeometry(100, 100, 800, 600) # x, y, width, height
window.show()
app.exec_()
QWidget
QWidget はすべてのユーザーインターフェースオブジェクトの基本クラスです:
サンプル
from PyQt5.QtWidgets import QWidget
widget = QWidget()
widget.setWindowTitle('基本ウィンドウ')
widget.show()
widget = QWidget()
widget.setWindowTitle('基本ウィンドウ')
widget.show()
よく使う入力コンポーネント
QLabel
テキストまたは画像を表示します:
サンプル
from PyQt5.QtWidgets import QLabel
label = QLabel('これはラベルです')
label.setStyleSheet('font-size: 16px; color: blue;')
label = QLabel('これはラベルです')
label.setStyleSheet('font-size: 16px; color: blue;')
QLineEdit
1行テキスト入力ボックス:
サンプル
from PyQt5.QtWidgets import QLineEdit
line_edit = QLineEdit()
line_edit.setPlaceholderText('内容を入力してください...')
line_edit = QLineEdit()
line_edit.setPlaceholderText('内容を入力してください...')
QTextEdit
複数行テキストエディター:
サンプル
from PyQt5.QtWidgets import QTextEdit
text_edit = QTextEdit()
text_edit.setPlainText('複数行テキストエディター')
text_edit = QTextEdit()
text_edit.setPlainText('複数行テキストエディター')
QComboBox
ドロップダウン選択ボックス:
サンプル
from PyQt5.QtWidgets import QComboBox
combo = QComboBox()
combo.addItems(['オプション1', 'オプション2', 'オプション3'])
combo = QComboBox()
combo.addItems(['オプション1', 'オプション2', 'オプション3'])
ボタン系コンポーネント
QPushButton
通常のボタン:
サンプル
from PyQt5.QtWidgets import QPushButton
button = QPushButton('クリックして')
button.setStyleSheet('background-color: green; color: white;')
button = QPushButton('クリックして')
button.setStyleSheet('background-color: green; color: white;')
QRadioButton
ラジオボタン:
サンプル
from PyQt5.QtWidgets import QRadioButton
radio1 = QRadioButton('オプション1')
radio2 = QRadioButton('オプション2')
radio1 = QRadioButton('オプション1')
radio2 = QRadioButton('オプション2')
QCheckBox
チェックボックス:
サンプル
from PyQt5.QtWidgets import QCheckBox
check1 = QCheckBox('選択1')
check2 = QCheckBox('選択2')
check1 = QCheckBox('選択1')
check2 = QCheckBox('選択2')
レイアウトコンポーネント
QHBoxLayout
水平レイアウト:
サンプル
from PyQt5.QtWidgets import QHBoxLayout, QPushButton
h_layout = QHBoxLayout()
h_layout.addWidget(QPushButton('左'))
h_layout.addWidget(QPushButton('中央'))
h_layout.addWidget(QPushButton('右'))
h_layout = QHBoxLayout()
h_layout.addWidget(QPushButton('左'))
h_layout.addWidget(QPushButton('中央'))
h_layout.addWidget(QPushButton('右'))
QVBoxLayout
垂直レイアウト:
サンプル
from PyQt5.QtWidgets import QVBoxLayout, QPushButton
v_layout = QVBoxLayout()
v_layout.addWidget(QPushButton('上'))
v_layout.addWidget(QPushButton('中央'))
v_layout.addWidget(QPushButton('下'))
v_layout = QVBoxLayout()
v_layout.addWidget(QPushButton('上'))
v_layout.addWidget(QPushButton('中央'))
v_layout.addWidget(QPushButton('下'))
QGridLayout
グリッドレイアウト:
サンプル
from PyQt5.QtWidgets import QGridLayout, QPushButton
grid = QGridLayout()
grid.addWidget(QPushButton('(0,0)'), 0, 0)
grid.addWidget(QPushButton('(0,1)'), 0, 1)
grid.addWidget(QPushButton('(1,0)'), 1, 0)
grid = QGridLayout()
grid.addWidget(QPushButton('(0,0)'), 0, 0)
grid.addWidget(QPushButton('(0,1)'), 0, 1)
grid.addWidget(QPushButton('(1,0)'), 1, 0)
上級コンポーネント
QTableWidget
テーブルコンポーネント:
サンプル
from PyQt5.QtWidgets import QTableWidget, QTableWidgetItem
table = QTableWidget(3, 3) # 3行3列
table.setHorizontalHeaderLabels(['列1', '列2', '列3'])
table.setItem(0, 0, QTableWidgetItem('データ1'))
table = QTableWidget(3, 3) # 3行3列
table.setHorizontalHeaderLabels(['列1', '列2', '列3'])
table.setItem(0, 0, QTableWidgetItem('データ1'))
QTreeWidget
ツリーコンポーネント:
サンプル
from PyQt5.QtWidgets import QTreeWidget, QTreeWidgetItem
tree = QTreeWidget()
tree.setHeaderLabels(['名前', '値'])
root = QTreeWidgetItem(tree, ['ルートノード'])
child = QTreeWidgetItem(root, ['子ノード', '123'])
tree = QTreeWidget()
tree.setHeaderLabels(['名前', '値'])
root = QTreeWidgetItem(tree, ['ルートノード'])
child = QTreeWidgetItem(root, ['子ノード', '123'])
QTabWidget
タブコンポーネント:
サンプル
from PyQt5.QtWidgets import QTabWidget, QWidget
tab = QTabWidget()
tab.addTab(QWidget(), 'タブ1')
tab.addTab(QWidget(), 'タブ2')
tab = QTabWidget()
tab.addTab(QWidget(), 'タブ1')
tab.addTab(QWidget(), 'タブ2')
ダイアログコンポーネント
QMessageBox
メッセージダイアログ:
サンプル
from PyQt5.QtWidgets import QMessageBox
msg = QMessageBox()
msg.setWindowTitle('ヒント')
msg.setText('これはメッセージダイアログです')
msg.setIcon(QMessageBox.Information)
msg.exec_()
msg = QMessageBox()
msg.setWindowTitle('ヒント')
msg.setText('これはメッセージダイアログです')
msg.setIcon(QMessageBox.Information)
msg.exec_()
QFileDialog
ファイルダイアログ:
サンプル
from PyQt5.QtWidgets import QFileDialog
file_name = QFileDialog.getOpenFileName(None, 'ファイルを選択', '', 'テキストファイル (*.txt);;すべてのファイル (*)')
file_name = QFileDialog.getOpenFileName(None, 'ファイルを選択', '', 'テキストファイル (*.txt);;すべてのファイル (*)')
スタイル設定
PyQt は CSS スタイルを使用してコンポーネントを美化することをサポートしています:
サンプル
button = QPushButton('スタイルボタン')
button.setStyleSheet('''
QPushButton {
background-color: #4C7EAF;
border: none;
color: white;
padding: 10px 24px;
text-align: center;
font-size: 16px;
margin: 4px 2px;
border-radius: 8px;
}
QPushButton:hover {
background-color: #4777A6;
}
''')
button.setStyleSheet('''
QPushButton {
background-color: #4C7EAF;
border: none;
color: white;
padding: 10px 24px;
text-align: center;
font-size: 16px;
margin: 4px 2px;
border-radius: 8px;
}
QPushButton:hover {
background-color: #4777A6;
}
''')