Python?pyqt5下拉多選框的實現(xiàn)示例
一、下拉列表框QComboBox
QComboBox是一個集按鈕和下拉選項于一體的控件,也稱為下拉列表框。
常用的方法
- addItem() 添加一個下拉選項
- addItems() 從列表中添加下拉選項
- Clear() 刪除下拉選項中集中的所有選項
- count() 返回下拉選項集合中的數(shù)目
- currentText() 返回選中選項的文本
- itemText(i) 獲取索引為i的item的選項文本
- currentIndex() 返回選中項的索引
- setItemText(int index, text) 改變序號為index項的文本
常用的信號
- Activated 當用戶選中一個下拉選項時發(fā)射該信號
- currentIndexChanged() 當下拉選項的索引發(fā)生改變時發(fā)射該信號
- highlighted 當選中一個已經(jīng)選中的下拉選項時,發(fā)射該信號
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))#每個選項名稱 print("Current index", i, "selection changed ", self.cb.currentText())#當前選項 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 = ['語文', '數(shù)學', '英語', '體育'] 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下拉多選框的實現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python pyqt5下拉多選框內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
pycharm 使用心得(六)進行簡單的數(shù)據(jù)庫管理
功能簡介:pycharm自帶了一個簡單的數(shù)據(jù)庫插件,可以比較方便的進行簡單的數(shù)據(jù)庫操作。2014-06-06Python3+Appium實現(xiàn)多臺移動設(shè)備操作的方法
這篇文章主要介紹了Python3+Appium實現(xiàn)多臺移動設(shè)備操作的方法,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-07-07如何在Win10系統(tǒng)使用Python3連接Hive
這篇文章主要介紹了如何在Win10系統(tǒng)使用Python3連接Hive,幫助大家更好的利用python讀取數(shù)據(jù),進行探索、分析和挖掘工作。感興趣的朋友可以了解下2020-10-10python腳本內(nèi)運行l(wèi)inux命令的方法
這篇文章主要介紹了python腳本內(nèi)運行l(wèi)inux命令的方法,實例分析了Python基于subprocess模塊操作Linux命令的相關(guān)技巧,需要的朋友可以參考下2015-07-07keras的load_model實現(xiàn)加載含有參數(shù)的自定義模型
這篇文章主要介紹了keras的load_model實現(xiàn)加載含有參數(shù)的自定義模型,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06pycharm遠程連接服務(wù)器調(diào)試tensorflow無法加載問題
最近打算在win系統(tǒng)下使用pycharm開發(fā)程序,并遠程連接服務(wù)器調(diào)試程序,其中在import tensorflow時報錯,本文就來介紹一下如何解決,感興趣的可以了解一下2021-06-06pygame開發(fā):馬賽邏輯小游戲的代碼實現(xiàn)
這篇文章主要介紹了pygame開發(fā),通過本文,您可以使用pygame開發(fā)一個馬賽邏輯小游戲~有需要的朋友可以借鑒參考下,希望能夠有所幫助2021-09-09使用wxPython和ECharts實現(xiàn)生成和保存HTML圖表
wxPython是一個基于wxWidgets的Python?GUI庫,ECharts是一個用于數(shù)據(jù)可視化的JavaScript庫,本文主要為大家介紹了如何使用wxPython和ECharts庫來生成和保存HTML圖表,感興趣的可以學習一下2023-08-08