Python?pyqt5下拉多選框的實(shí)現(xiàn)示例
一、下拉列表框QComboBox
QComboBox是一個(gè)集按鈕和下拉選項(xiàng)于一體的控件,也稱為下拉列表框。
常用的方法
- addItem() 添加一個(gè)下拉選項(xiàng)
- addItems() 從列表中添加下拉選項(xiàng)
- Clear() 刪除下拉選項(xiàng)中集中的所有選項(xiàng)
- count() 返回下拉選項(xiàng)集合中的數(shù)目
- currentText() 返回選中選項(xiàng)的文本
- itemText(i) 獲取索引為i的item的選項(xiàng)文本
- currentIndex() 返回選中項(xiàng)的索引
- setItemText(int index, text) 改變序號(hào)為index項(xiàng)的文本
常用的信號(hào)
- Activated 當(dāng)用戶選中一個(gè)下拉選項(xiàng)時(shí)發(fā)射該信號(hào)
- currentIndexChanged() 當(dāng)下拉選項(xiàng)的索引發(fā)生改變時(shí)發(fā)射該信號(hào)
- highlighted 當(dāng)選中一個(gè)已經(jīng)選中的下拉選項(xiàng)時(shí),發(fā)射該信號(hào)
import sys from PyQt5.QtCore import * from PyQt5.QtGui import * from PyQt5.QtWidgets import * class ComboxDemo(QWidget): def __init__(self, parent=None): super(ComboxDemo, self).__init__(parent) self.setWindowTitle("combox 例子") self.resize(300, 90) layout = QVBoxLayout() self.lbl = QLabel("") 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) layout.addWidget(self.lbl) self.setLayout(layout) def selectionchange(self, i): self.lbl.setText(self.cb.currentText()) self.lbl.adjustSize() print("Items in the list are :") for count in range(self.cb.count()): print('item' + str(count) + '=' + self.cb.itemText(count))#每個(gè)選項(xiàng)名稱 print("Current index", i, "selection changed ", self.cb.currentText())#當(dāng)前選項(xiàng) if __name__ == '__main__': app = QApplication(sys.argv) comboxDemo = ComboxDemo() comboxDemo.show() sys.exit(app.exec_())
二、下拉多選
from PyQt5.QtWidgets import QComboBox, QListWidgetItem, QListWidget, QCheckBox, \ QApplication, QVBoxLayout, QWidget,QPushButton,QHBoxLayout,QLineEdit import sys class ComboCheckBox(QWidget): def __init__(self, parent=None): super(ComboCheckBox, self).__init__(parent) self.items = ['語(yǔ)文', '數(shù)學(xué)', '英語(yǔ)', '體育'] self.box_list = [] self.comb = QComboBox(self) #列表框QComboBox self.listwidget = QListWidget(self) #(列表控件) for i in range(len(self.items)): self.box_list.append(QCheckBox(self)) self.box_list[i].setText(self.items[i]) item = QListWidgetItem(self.listwidget) self.listwidget.setItemWidget(item, self.box_list[i]) # QComboBox添加模型和視圖,QListWidget設(shè)置為QComboBox的View,QListWidget的Model設(shè)置為QComboBox的Model self.comb.setModel(self.listwidget.model()) self.comb.setView(self.listwidget) self.btn = QPushButton('選擇', self) self.btn.clicked.connect(self.get_selected) self.line=QLineEdit() #布局 layout = QVBoxLayout() layout.addWidget(self.btn) layout.addWidget(self.comb) layout.addWidget(self.line) self.setLayout(layout) def get_selected(self): ret = [] for i in range(len(self.items)): if self.box_list[i].isChecked(): ret.append(self.box_list[i].text()) self.line.setText(str(ret)) if __name__ == "__main__": app = QApplication(sys.argv) ui = ComboCheckBox() ui.show() sys.exit(app.exec_())
到此這篇關(guān)于Python pyqt5下拉多選框的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python pyqt5下拉多選框內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm 使用心得(六)進(jìn)行簡(jiǎn)單的數(shù)據(jù)庫(kù)管理
功能簡(jiǎn)介:pycharm自帶了一個(gè)簡(jiǎn)單的數(shù)據(jù)庫(kù)插件,可以比較方便的進(jìn)行簡(jiǎn)單的數(shù)據(jù)庫(kù)操作。2014-06-06《Python之禪》中對(duì)于Python編程過(guò)程中的一些建議
這篇文章主要介紹了《Python之禪》中對(duì)于Python編程過(guò)程中的一些建議,需要的朋友可以參考下2015-04-04Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法
這篇文章主要介紹了Python3+Appium實(shí)現(xiàn)多臺(tái)移動(dòng)設(shè)備操作的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07如何在Win10系統(tǒng)使用Python3連接Hive
這篇文章主要介紹了如何在Win10系統(tǒng)使用Python3連接Hive,幫助大家更好的利用python讀取數(shù)據(jù),進(jìn)行探索、分析和挖掘工作。感興趣的朋友可以了解下2020-10-10python腳本內(nèi)運(yùn)行l(wèi)inux命令的方法
這篇文章主要介紹了python腳本內(nèi)運(yùn)行l(wèi)inux命令的方法,實(shí)例分析了Python基于subprocess模塊操作Linux命令的相關(guān)技巧,需要的朋友可以參考下2015-07-07keras的load_model實(shí)現(xiàn)加載含有參數(shù)的自定義模型
這篇文章主要介紹了keras的load_model實(shí)現(xiàn)加載含有參數(shù)的自定義模型,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-06-06pycharm遠(yuǎn)程連接服務(wù)器調(diào)試tensorflow無(wú)法加載問(wèn)題
最近打算在win系統(tǒng)下使用pycharm開(kāi)發(fā)程序,并遠(yuǎn)程連接服務(wù)器調(diào)試程序,其中在import tensorflow時(shí)報(bào)錯(cuò),本文就來(lái)介紹一下如何解決,感興趣的可以了解一下2021-06-06pygame開(kāi)發(fā):馬賽邏輯小游戲的代碼實(shí)現(xiàn)
這篇文章主要介紹了pygame開(kāi)發(fā),通過(guò)本文,您可以使用pygame開(kāi)發(fā)一個(gè)馬賽邏輯小游戲~有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09使用wxPython和ECharts實(shí)現(xiàn)生成和保存HTML圖表
wxPython是一個(gè)基于wxWidgets的Python?GUI庫(kù),ECharts是一個(gè)用于數(shù)據(jù)可視化的JavaScript庫(kù),本文主要為大家介紹了如何使用wxPython和ECharts庫(kù)來(lái)生成和保存HTML圖表,感興趣的可以學(xué)習(xí)一下2023-08-08