pyqt设置combobox
时间: 2023-10-07 22:10:34 浏览: 44
设置 `combobox` 的步骤如下:
1. 在 Qt Designer 中将 `combobox` 拖拽到界面上。
2. 在代码中导入 `QComboBox` 类。
```python
from PyQt5.QtWidgets import QComboBox
```
3. 创建 `combobox` 对象。
```python
combobox = QComboBox()
```
4. 添加选项。
```python
combobox.addItem("选项1")
combobox.addItem("选项2")
combobox.addItem("选项3")
```
5. 设置默认选中项。
```python
combobox.setCurrentIndex(0)
```
6. 将 `combobox` 添加到布局中。
```python
layout.addWidget(combobox)
```
完整代码示例:
```python
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox
app = QApplication([])
window = QWidget()
layout = QVBoxLayout()
# 创建 combobox 对象
combobox = QComboBox()
# 添加选项
combobox.addItem("选项1")
combobox.addItem("选项2")
combobox.addItem("选项3")
# 设置默认选中项
combobox.setCurrentIndex(0)
# 将 combobox 添加到布局中
layout.addWidget(combobox)
window.setLayout(layout)
window.show()
app.exec_()
```