效果圖:
ui_ComboBox.py

# -*- coding: utf-8 -*- # Form implementation generated from reading ui file 'ui_ComboBox.ui' # # Created by: PyQt5 UI code generator 5.13.0 # # WARNING! All changes made in this file will be lost! from PyQt5 import QtCore, QtGui, QtWidgets class Ui_Form(object): def setupUi(self, Form): Form.setObjectName("Form") Form.resize(433, 193) self.groupBox = QtWidgets.QGroupBox(Form) self.groupBox.setGeometry(QtCore.QRect(10, 10, 411, 71)) self.groupBox.setObjectName("groupBox") self.lineEdit = QtWidgets.QLineEdit(self.groupBox) self.lineEdit.setGeometry(QtCore.QRect(10, 30, 391, 20)) self.lineEdit.setObjectName("lineEdit") self.groupBox_2 = QtWidgets.QGroupBox(Form) self.groupBox_2.setGeometry(QtCore.QRect(10, 89, 231, 91)) self.groupBox_2.setObjectName("groupBox_2") self.btnIniItems = QtWidgets.QPushButton(self.groupBox_2) self.btnIniItems.setGeometry(QtCore.QRect(10, 30, 75, 21)) self.btnIniItems.setObjectName("btnIniItems") self.pushButton_2 = QtWidgets.QPushButton(self.groupBox_2) self.pushButton_2.setGeometry(QtCore.QRect(90, 30, 61, 21)) self.pushButton_2.setObjectName("pushButton_2") self.chkBoxEditable = QtWidgets.QCheckBox(self.groupBox_2) self.chkBoxEditable.setGeometry(QtCore.QRect(170, 30, 70, 17)) self.chkBoxEditable.setObjectName("chkBoxEditable") self.comboBox = QtWidgets.QComboBox(self.groupBox_2) self.comboBox.setGeometry(QtCore.QRect(10, 60, 211, 22)) self.comboBox.setObjectName("comboBox") self.groupBox_3 = QtWidgets.QGroupBox(Form) self.groupBox_3.setGeometry(QtCore.QRect(250, 89, 171, 91)) self.groupBox_3.setObjectName("groupBox_3") self.btnIni2 = QtWidgets.QPushButton(self.groupBox_3) self.btnIni2.setGeometry(QtCore.QRect(10, 30, 151, 23)) self.btnIni2.setObjectName("btnIni2") self.comboBox_2 = QtWidgets.QComboBox(self.groupBox_3) self.comboBox_2.setGeometry(QtCore.QRect(10, 60, 151, 22)) self.comboBox_2.setObjectName("comboBox_2") self.retranslateUi(Form) QtCore.QMetaObject.connectSlotsByName(Form) def retranslateUi(self, Form): _translate = QtCore.QCoreApplication.translate Form.setWindowTitle(_translate("Form", "Form")) self.groupBox.setTitle(_translate("Form", "選擇的內容")) self.groupBox_2.setTitle(_translate("Form", "簡單的ComboBox")) self.btnIniItems.setText(_translate("Form", "初始化列表")) self.pushButton_2.setText(_translate("Form", "清除列表")) self.chkBoxEditable.setText(_translate("Form", "可編輯")) self.groupBox_3.setTitle(_translate("Form", "有用戶數據的ComboBox")) self.btnIni2.setText(_translate("Form", "初始化城市+區號"))
myWidget_ComboBox.py

#!/usr/bin/env python # _*_ coding: UTF-8 _*_ """================================================= @Project -> File : Operate-system -> myWidget_ComboBox.py @IDE : PyCharm @Author : zihan @Date : 2020/4/11 9:48 @Desc : =================================================""" import sys from PyQt5.QtWidgets import QApplication, QWidget from PyQt5.QtCore import pyqtSlot from PyQt5.QtGui import QIcon from ui_ComboBox import Ui_Form class QmyWidget(QWidget): def __init__(self, parent=None): super().__init__(parent) # 調用父類構造函數,創建窗體 self.ui = Ui_Form() # 創建UI對象 self.ui.setupUi(self) # 構造UI self.ui.btnIni2.clicked.connect(self.on_btn_ini2_clicked) self.ui.comboBox_2.currentIndexChanged[str].connect(self.combo_box_current_index_change) def on_btnIniItems_clicked(self): # 初始化列表按鈕 icon = QIcon("./icons/images/aim.ico") self.ui.comboBox.clear() # 清除列表 provinces = ["山東", "河北", "河南", "湖北", "湖南", "廣東"] # 列表數據 # self.ui.comboBox.addItems(provinces) # 直接添加列表,但無法加圖標 for i in range(len(provinces)): self.ui.comboBox.addItem(icon, provinces[i]) @pyqtSlot(bool) # 可編輯CheckBox def on_chkBoxEditable_clicked(self, checked): self.ui.comboBox.setEditable(checked) @pyqtSlot(str) # 簡單的ComboBox的當前項變化 def on_comboBox_currentIndexChanged(self, curText): self.ui.lineEdit.setText(curText) def on_btn_ini2_clicked(self): # 有用戶數據的comboBox2的初始化 icon = QIcon("./icons/images/unit.ico") self.ui.comboBox_2.clear() cities = {"北京": 10, "上海": 21, "天津": 22, "徐州": 516, "福州": 591, "青島": 532} # 字典數據 for k in cities: # print(k) self.ui.comboBox_2.addItem(icon, k, cities[k]) def combo_box_current_index_change(self, curText): self.ui.lineEdit.setText(curText) zone = self.ui.comboBox_2.currentData() # 讀取關聯數據 if zone is not None: self.ui.lineEdit.setText(curText + ":區號=%d" % zone) if __name__ == "__main__": app = QApplication(sys.argv) # 創建app,用QApplication類 form = QmyWidget() form.show() sys.exit(app.exec_())
里面的ico圖標自己從網上下就可以,我也是隨便找的兩個圖標,哈哈哈。此系列的學習內容均來自《Python Qt GUI與數據化可視化編程》這本書,然后自己爬坑修改。
特別提醒:盡量不要使用自帶的那種on_<objectname>_<...>這類內建的函數名,雖然不需要自己手動建立connect,但是有時會有意外的情況發生,比如你單擊一次pushButton,你發現clicked函數執行了兩次。這是經過了實驗得到的結果,所以最穩妥的辦法還是自己建立連接,自己命名槽函數,就不會出現意外情況了,關於這種情況也可以參考:https://blog.csdn.net/zhujinghao_09/article/details/8476453
QComboBox常用函數總結
QComboBox存儲的項是一個列表,但是QComboBox不提供整個列表用於訪問,而可以通過索引訪問某個項。訪問項的一些函數主要有以下幾個。
currentIndex():返回當前項的序號,第一項的序號為0。
currentText():返回當前項的文字。
currentData(role):返回當前項的關聯數據,參數role表示數據角色,角色role的默認值為Qt.UserRole。可以為一個項定義多個角色的用戶數據,更多自定義角色的編號從Qt.UserRole開始增加,如Qt.UserRole+1、Qt.UserRole+2.
itemText(index):返回索引號為index的項的文字。
itemData(index, role):返回索引號為index的項的角色為role的關聯數據,角色role的默認值為Qt.UserRole。
count():返回項的個數。
ok.
1. 設置選中的字體居中,參考鏈接:https://blog.csdn.net/qq_18286031/article/details/85113931
m_lineedit = QLineEdit()
m_lineedit.setReadOnly(True)
m_lineedit.setAlignment(Qt.AlignCenter)
self.ui.comboBox.setLineEdit(m_lineedit)
效果: