In this tutorial, I am going to show you how to create a combo box widget with PyQt5 in Python.
If you have never used a combo box before, a combo box is just a drop-down with a list of items a user can choose from.
Buy Me a Coffee? Your support is much appreciated!
PayPal Me: https://www.paypal.me/jiejenn/5
Venmo: @Jie-Jenn
Full code
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QComboBox, QPushButton
class ComboBoxDemo(QWidget):
def __init__(self):
super().__init__()
lstCompany = ['Google', 'Facebook', 'Microsoft', 'Apple', 'Uber']
self.comboBox = QComboBox(self)
self.comboBox.setGeometry(50, 50, 400, 35)
self.comboBox.addItems(lstCompany)
self.btn = QPushButton('Click', self)
self.btn.setGeometry(170, 120, 120, 35)
self.btn.clicked.connect(self.getComboValue)
def getComboValue(self):
print((self.comboBox.currentText(), self.comboBox.currentIndex()))
app = QApplication(sys.argv)
demo = ComboBoxDemo()
demo.show()
sys.exit(app.exec_())