Python

pyqt combobox

A combobox can be created with the QComboBox class. Items are added using the class method addItem, followed by a string parameter.

Sometimes its called a listbox, as it shows a list of items a user can select from. If you want a user to choose from a set of items, the listbox is the best option.

pyqt combobox

Related course:
Create Simple GUI Applications with Python and Qt

Combobox example

The code below creates a combobox using the class QComboBox.

import sys
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMainWindow, QLabel, QGridLayout, QWidget, QComboBox
from PyQt5.QtCore import QSize, QRect

class ExampleWindow(QMainWindow):
def __init__(self):
QMainWindow.__init__(self)

self.setMinimumSize(QSize(640, 140))
self.setWindowTitle("Combobox example")

centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)

# Create combobox and add items.
self.comboBox = QComboBox(centralWidget)
self.comboBox.setGeometry(QRect(40, 40, 491, 31))
self.comboBox.setObjectName(("comboBox"))
self.comboBox.addItem("PyQt")
self.comboBox.addItem("Qt")
self.comboBox.addItem("Python")
self.comboBox.addItem("Example")

if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
mainWin = ExampleWindow()
mainWin.show()
sys.exit( app.exec_() )

Previous Post Next Post

Cookie policy | Privacy policy | ©

PyQt