Python Forum
[PyQt] [Solved]Populate ComboBox with for loop?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] [Solved]Populate ComboBox with for loop?
#1
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)
        #------------------------------------------
Reply
#2
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_())
Reply
#3
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)
Reply
#4
Thanks!

I was able to do it with isinstance.

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


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] populate a QListWidget devilonline 1 1,610 Apr-10-2023, 02:52 AM
Last Post: deanhystad
  [PyQt] [Solved]Help getting out of loop Extra 5 3,825 Sep-30-2022, 11:27 PM
Last Post: Extra
  [PyQt] [Solved]Help Adding Sql Results in ComboBox Extra 2 1,255 Jul-07-2022, 09:46 PM
Last Post: Extra
  [PyQt] [Solved]Help Adding results from for loop Extra 2 1,472 Jun-24-2022, 05:01 PM
Last Post: Extra
  Problems getting tk Combobox contents to populate properly dford 4 3,840 Jan-08-2022, 02:39 PM
Last Post: dford
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,403 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt
  Auto populate dictionary with names/values of QT widgets cjh 1 2,956 Mar-23-2021, 02:56 PM
Last Post: deanhystad
  [PyQt] How to populate a treeview on a GUI with a dictionary mart79 1 8,280 Aug-05-2019, 01:30 PM
Last Post: Denni
  populate list with images and be able to select them ricardons 0 2,137 Jan-11-2019, 03:45 PM
Last Post: ricardons
  pyqt5 combobox add item with loop shift838 5 9,631 Dec-09-2018, 08:54 PM
Last Post: shift838

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020