from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QComboBox
class DemoApp(QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
layout = QVBoxLayout()
# 创建一个 QComboBox 实例
self.comboBox = QComboBox(self)
# 为 QComboBox 添加四个选项
self.comboBox.addItem("选项1")
self.comboBox.addItem("选项2")
self.comboBox.addItem("选项3")
self.comboBox.addItem("选项4")
# 将 QComboBox 添加到布局中
layout.addWidget(self.comboBox)
self.setLayout(layout)
self.setWindowTitle('QComboBox 示例')
self.setGeometry(300, 300, 200, 100)
self.show()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
demo = DemoApp()
sys.exit(app.exec_())
10-04
4791
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
12-20
4838
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
10-04
665
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
08-20