Python Forum

Full Version: [Solved]Populate ComboBox with for loop?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

I'm trying to populate 22 combo boxes with the same options (I have 'Material_1_ComboBox' all the way to 'Material_22_ComboBox') and I want to know how to do populate all those combo boxes with my 'stringMaterialList' using a for loop instead of going:
self.Material_1_ComboBox.addItems(StringMaterialList)
self.Material_2_ComboBox.addItems(StringMaterialList)
self.Material_3_ComboBox.addItems(StringMaterialList)
...
...
self.Material_22_ComboBox.addItems(StringMaterialList)


How would I go about doing that?

the part that I'm having trouble with is thinking about how I would iterate through the names so it just changes the number in the middle, therefore populating all 22 ComboBoxes.

Thanks in advance.

Snippet
        #------------------------------------------
        #           Material Dropdown
        #------------------------------------------
        #Connect to the Inventory database
        connection = sqlite3.connect(InventoryDatabase)
        cursor = connection.cursor()
        cursor.execute('''
            SELECT Name From Items
            ''')
        connection.commit()
        
        #Get the Categories from the Database
        MaterialList = cursor.fetchall()

        #Convert the SQL Results to a String List
        from itertools import chain
        StringMaterialList = list(chain.from_iterable(MaterialList))
     
        #Close the connection
        connection.close()

        #Add the SQL Results to the Dropdown List
        self.Material_1_ComboBox.addItems(StringMaterialList)
        #------------------------------------------
You can use isinstance

from sys import exit as sys_exit

from PyQt5.QtWidgets import (QApplication, QMainWindow, 
                                QHBoxLayout, QComboBox, QWidget)

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

        self.setGeometry(150, 150, 200, 100)
        self.cmb_items = ['1', '2', '3', '4']

        self.cmb_box_1 = QComboBox()
        self.cmb_box_2 = QComboBox()

        HBox = QHBoxLayout()
        HBox.addWidget(self.cmb_box_1)
        HBox.addStretch(1)
        HBox.addWidget(self.cmb_box_2)

        self.cw = QWidget()
        self.cw.setLayout(HBox)
        
        self.setCentralWidget(self.cw)
        
        for widget in self.cw.children():
            if isinstance(widget, QComboBox):
                widget.addItems(self.cmb_items)


if __name__ == '__main__':
    app = QApplication([])
    win = MainWindow()
    win.show()
    sys_exit(app.exec_())
for cbox in (
    self.Material_1_ComboBox,
    . . .
    self.Material_22_ComboBox):
    cbox.addItems(StringMaterialList)
Why do you have 22 combo boxes that have the same choices? If you really need 22 similar comb boxes you should use a list:
material_choices = [However_you_make_a_combo_box() for _ in range(22)]
And now that all the similar combo boxes are in a list you just do this:
for cbox in material_choices:
    cbox.addItems(StringMaterialList)
Thanks!

I was able to do it with isinstance.

        for widget in self.MaterialUsedFrame.children():
            if isinstance(widget, QComboBox):
                widget.addItems(StringMaterialList)