以下是一些常见的 PyQt5 widget 的用法示例:
1. QPushButton(按钮)
import sys
from PyQt5.QtWidgets import QApplication, QPushButton, QWidget
def button_clicked():
print("Button clicked!")
app = QApplication(sys.argv)
window = QWidget()
button = QPushButton('Click me', window)
button.clicked.connect(button_clicked)
window.show()
sys.exit(app.exec_())
2. QLabel(标签)
import sys
from PyQt5.QtWidgets import QApplication, QLabel, QWidget
app = QApplication(sys.argv)
window = QWidget()
label = QLabel('Hello, PyQt!', window)
label.move(50, 50)
window.setGeometry(100, 100, 300, 200)
window.show()
sys.exit(app.exec_())
3. QLineEdit(单行文本框)
import sys
from PyQt5.QtWidgets import QApplication, QLineEdit, QWidget, QVBoxLayout
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
line_edit = QLineEdit()
layout.addWidget(line_edit)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
4. QComboBox(下拉框)
import sys
from PyQt5.QtWidgets import QApplication, QComboBox, QWidget, QVBoxLayout
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
combo_box = QComboBox()
combo_box.addItems(["Option 1", "Option 2", "Option 3"])
layout.addWidget(combo_box)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
5. QCheckBox(复选框)
import sys
from PyQt5.QtWidgets import QApplication, QCheckBox, QWidget, QVBoxLayout
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
checkbox = QCheckBox('Check me', window)
layout.addWidget(checkbox)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
6. QSpinBox(数字选择框)
import sys
from PyQt5.QtWidgets import QApplication, QSpinBox, QWidget, QVBoxLayout
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
spinbox = QSpinBox()
layout.addWidget(spinbox)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
7. QSlider(滑块)
import sys
from PyQt5.QtWidgets import QApplication, QSlider, QWidget, QVBoxLayout
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
slider = QSlider()
slider.setOrientation(Qt.Horizontal)
layout.addWidget(slider)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
这些示例展示了如何使用 PyQt5 创建常见的界面组件。通过组合这些组件及其信号和槽,你可以构建出丰富多彩的用户界面。
8. QProgressBar(进度条)
进度条是用来展示任务进度的组件。以下示例展示如何创建并更新一个进度条。
import sys
from PyQt5.QtWidgets import QApplication, QProgressBar, QWidget, QVBoxLayout, QPushButton
def update_progress():
current_value = progress_bar.value()
max_value = progress_bar.maximum()
if current_value < max_value:
progress_bar.setValue(current_value + 10)
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
progress_bar = QProgressBar()
progress_bar.setMaximum(100)
layout.addWidget(progress_bar)
button = QPushButton("Increase Progress")
button.clicked.connect(update_progress)
layout.addWidget(button)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
9. QDialog(对话框)
对话框通常用于弹出窗口,以便在需要额外信息或确认时与用户交互。
import sys
from PyQt5.QtWidgets import QApplication, QDialog, QDialogButtonBox, QVBoxLayout, QLabel
app = QApplication(sys.argv)
dialog = QDialog()
layout = QVBoxLayout(dialog)
label = QLabel("This is a dialog.", dialog)
layout.addWidget(label)
buttons = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
layout.addWidget(buttons)
buttons.accepted.connect(dialog.accept)
buttons.rejected.connect(dialog.reject)
dialog.setLayout(layout)
dialog.show()
result = dialog.exec_() # 这将阻塞,直到用户关闭对话框
print("Dialog result:", result)
sys.exit(app.exec_())
10. QTreeView(树视图)和QStandardItemModel
树视图通常用于显示具有层级结构的数据。以下示例演示如何使用QStandardItemModel
创建并显示一个简单的树。
import sys
from PyQt5.QtWidgets import QApplication, QTreeView, QStandardItemModel, QStandardItem
app = QApplication(sys.argv)
tree_view = QTreeView()
model = QStandardItemModel()
model.setHorizontalHeaderLabels(['Title'])
root_node = QStandardItem('Root Node')
child_node = QStandardItem('Child Node')
root_node.appendRow(child_node)
model.appendRow(root_node)
tree_view.setModel(model)
tree_view.expandAll()
tree_view.show()
sys.exit(app.exec_())
11. QTableWidget(表格)
表格是用来显示和编辑二维数据的组件。以下示例展示如何创建一个表格并添加数据。
import sys
from PyQt5.QtWidgets import QApplication, QTableWidget, QTableWidgetItem, QVBoxLayout, QWidget
app = QApplication(sys.argv)
window = QWidget()
layout = QVBoxLayout(window)
table_widget = QTableWidget(5, 3) # 5行3列
layout.addWidget(table_widget)
for i in range(5):
for j in range(3):
item = QTableWidgetItem(f"Item {i+1}, {j+1}")
table_widget.setItem(i, j, item)
window.setLayout(layout)
window.show()
sys.exit(app.exec_())
这些示例只是 PyQt5 功能的一部分。根据你的应用程序需求,PyQt5 还提供了更多高级功能和定制选项。