In this tutorial, I'm going to show you about creating a Combo Box, Adding CSS styles, and Getting value from Combo Box. You need to create these kinds of Combo Box to set a selection of multiple selections.
Create A Combo Box
I'm using QComboBox() function to create a combo box in PyQt5. You can add items to Combo Box using the addItems() function. You also can use the addItems() function, but it is used to set just one item. we can learn about it more with the below examples.
combo = QComboBox() -- I created my Combo Box using this
list = ["My Item1", "My Item2", "My Item3"] -- I'm using a list to add multiple items at once. because addItems() function can read every item in a list one by one.
combo.addItems(list) -- Using the addItems, I added items that we created to the combo box.
combo.setGeometry(100, 100, 60, 20) -- I set combo box place and size using this.
Adding CSS Styles to Combo Box
If you follow my PyQt5 post series, you know that we talked a lot more things about CSS styles in PyQt5. CSS supporting is an incredible feature I'm experienced in PyQt5 because it is making your plain widgets much more attractive. Now, I also going to make our plain Combo Box a bit attractive with CSS
Getting a Value From Combo Box
Now, we have a beautiful combo box, but how do you get the current value when someone clicks an item you create. If I explain it simply how you get an item that your user or someone selected. However, we can solve that problem using activated.connect() with currentText() function in QComboBox. This is how to use it.
activated.connect() -- this function can be used to connect your combo box to another task when the user switch between Combo Box items.
If you have any problems with this tutorial, feel free to comment below.
0 Comments