PyQt5 Tutorials
About Lesson

In this Python PyQt5 lesson we want to learn about Python PyQt5 QComboBox, using QComboBox you can add a selection widget that displays the current items. 

 

Now open your Qt Designer,  you can just write pyqt5designer in your terminal, after opening the Qt Designer you need to create Widget window. now we add widgets in Qt Designer.

  • Add a Vertical Layout
  • In the vertical layout add a combobox
  • Right click on the combobox and add items to the combobox 
  • Also add a label in the VBoxLayout

 

 

This is the design.

PyQt5 ComboBox
PyQt5 ComboBox

 

 

 

This is the ui file for the combobox.

 

 

OK now we want to load our ui file, so for this copy the ui file and paste to your directory, after that create a new python file, iam going to call it LoadCombo.py and add this code, for loading the ui file we want to use uic module from Python PyQt5.

 

 

You can see in the above code we have used loadUI() method for loading the ui file.

 

 

In our main design we have a QComboBox with a QLabel, so we need to find these widgets in the ui file, as we have done in the above code, we can use findChild() method for finding the widgets in ui file.

 

 

Also we have connected our currentTextChanged() signal of combobox with the combo_selected() method.

 

 

And this is the method that we have already connected with the signal, in this method we are going to just get the text from the combobox and we set the text in the label using setText() method.

 

 

 

Run the complete code and this is the result.

PyQt5 ComboBox - Working with QComboBox in PyQt
PyQt5 ComboBox – Working with QComboBox in PyQt

 

 

 

Creating PyQt QComboBox with Coding 

OK now we want to create our Python PyQt5 QComboBox using coding, in the previous part we have used Qt Designer, in here we are not going to use Qt Designer. this is the complete source code.

 

 

These are our window requirements like window title, window icon, width and height of the window, x and y position of the window.

 

 

So you can use QComboBox class object for creating combobox in pyqt.

 

 

This is the currentTextChanged() signal that is connected with the combo_selected() method.

 

 

This is the method that we have already connected with the currentTextChanged signal of combobox. so in this method first we need to get the item value from PyQt5 QComboBox and after that we set the value in Python PyQt5 label.

 

 

 

Run the complete code and this is the result.

Working with QComboBox in PyQt
Working with QComboBox in PyQt