文档:https://doc.qt.io/qtforpython-5/PySide2/QtWidgets/QComboBox.html
秘籍:https://www.tutorialspoint.com/pyqt/pyqt_qcombobox_widget.htm
下面秘籍真是喜欢,有例子,例子几乎涵盖了我的使用需求
import sys
from PyQt5.Qt import *
class combodemo(QWidget):
def __init__(self, parent=None):
super(combodemo, self).__init__(parent)
layout = QHBoxLayout()
self.cb = QComboBox()
self.cb.addItem("C")
self.cb.addItem("C++")
self.cb.addItems(["Java", "C#", "Python"])
self.cb.currentIndexChanged.connect(self.selectionchange)
layout.addWidget(self.cb)
self.setLayout(layout)
self.setWindowTitle("combo box demo")
def selectionchange(self, i):
print("Items in the list are :")
for count in range(self.cb.count()):
print
self.cb.itemText(count)
print("Current index", i, "selection changed ", self.cb.currentText())
def main():
app = QApplication(sys.argv)
ex = combodemo()
ex.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
from PyQt5 import QtCore, QtGui, QtWidgets
import sys,os
class Window(object):
def __init__(self):
self.ui_top_menu(Form)
self.cb_seqList_addItem()
def ui_top_menu(self,Form):
# 创建一个横向布局(排列顶部镜头项目选择控件)
topMenuVLayout = QtWidgets.QHBoxLayout(Form)
# 创建下拉列表
self.cb_seqList = QtWidgets.QComboBox()
self.cb_seqList.setMinimumSize(QtCore.QSize(65, 21))
self.cb_seqList.setMaximumSize(QtCore.QSize(65, 21))
self.cb_scList = QtWidgets.QComboBox()
self.cb_scList.setMinimumSize(QtCore.QSize(105, 21))
self.cb_scList.setMaximumSize(QtCore.QSize(105, 21))
self.btn_expand = QtWidgets.QPushButton("=")
self.btn_expand.setMinimumSize(QtCore.QSize(15, 21))
self.btn_expand.setMaximumSize(QtCore.QSize(15, 21))
self.cb_proList = QtWidgets.QComboBox()
self.cb_proList.setMinimumSize(QtCore.QSize(85, 21))
self.cb_proList.setMaximumSize(QtCore.QSize(85, 21))
self.cb_proList.addItem("XCM_2022DY")
null = QtWidgets.QLabel()
null.setMinimumSize(QtCore.QSize(85, 21))
# 关联布局
topMenuVLayout.addWidget(self.cb_seqList, 10)
topMenuVLayout.addWidget(self.cb_scList, 20)
topMenuVLayout.addWidget(self.btn_expand, 0)
topMenuVLayout.addWidget(null, 0)
topMenuVLayout.addWidget(self.cb_proList, 15)
#信号与槽
self.cb_seqList.currentIndexChanged.connect(self.cb_scList_addItem)
def cb_seqList_addItem(self):
path = r"D:\FILM\XCM_2022DY\Shots"
file = os.listdir(path)
for each in file:
self.cb_seqList.addItem(each)
def cb_scList_addItem(self, i):
path = r"D:\FILM\XCM_2022DY\Shots"
file = os.listdir(os.path.join(path, self.cb_seqList.currentText()))
self.cb_scList.clear()
for each in file:
self.cb_scList.addItem(each)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
Form.resize(300, 300)
ui = Window()
Form.show()
sys.exit(app.exec_())
# Panel,Raised