PyQt5 – 如何向 ComboBox 添加项目?
在本文中,我们将了解如何将项目添加到组合框中。默认情况下,组合框没有项目,以便将项目添加到组合框中,我们使用addItem
方法。然后,组合框将允许我们从添加的项目中进行选择。
Syntax : combo_box.addItem(item)
Argument : It takes string as argument
Action performed : It will add the item to the drop down list of combo box
下面是实现
# importing libraries
from PyQt5.QtWidgets import *
from PyQt5 import QtCore, QtGui
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import sys
class Window(QMainWindow):
def __init__(self):
super().__init__()
# setting title
self.setWindowTitle("Python ")
# setting geometry
self.setGeometry(100, 100, 600, 400)
# calling method
self.UiComponents()
# showing all the widgets
self.show()
# method for widgets
def UiComponents(self):
# creating a combo box widget
combo_box = QComboBox(self)
# setting geometry of combo box
combo_box.setGeometry(200, 150, 120, 40)
# adding items to combo box
# adding Geek to the combobox
combo_box.addItem("Geek")
# adding Super Geek to the combobox
combo_box.addItem("Super Geek")
# adding Ultra Geek to the combobox
combo_box.addItem("Ultra Geek")
# create pyqt5 app
App = QApplication(sys.argv)
# create the instance of our Window
window = Window()
# start the app
sys.exit(App.exec())
输出 :