Python Forum

Full Version: pyqt5 combobox add item with loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Is there a way to use a for loop to add items to comboboxes. All items would be the same in each combobox.

I thought the below might work but nada:

x equals the combobox #, all have the same name but just increase in numbers. Comboboxes are named 'comboboxslot2' to 'comboboxslot8'.

I want to add the same items via a loop based on the selection that is made in the 'comborom' object.

#!/usr/bin/python3
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + "ooeygui.ui", self)
        self.ui.comborom.currentTextChanged.connect(self._pullcomboromText)
        self.ui.checkboxsb.stateChanged.connect(self._pullcheckboxsb)
        self.ui.buttonlaunch.clicked.connect(self._buttonLaunch)
        self.ui.checkBoxhexbusfloppy.stateChanged.connect(self._pullcheckboxhbfloppy)
        self.ui.checkBoxhexbusrs232.stateChanged.connect(self._pullcheckboxhbrs232)

        self.show()

    def _pullcomboromText(self, currentText):
        global sr
        if currentText == "Myarc Geneve 9640" or currentText == "Myarc Geneve 9640 (GenMod)":
            for x in range(2,9):
            self.ui.comboboxslot(x).addItem("item1")
            self.ui.comboboxslot(x).addItem("item2")
            self.ui.comboboxslot(x).addItem("item3")
            self.ui.comboboxslot(x).addItem("item4")
            self.ui.comboboxslot(x).addItem("item5")
            self.ui.comboboxslot(x).addItem("item6")
            self.ui.comboboxslot(x).addItem("item7")
            

if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
I get an error stating: AttributeError: 'Main' object has no attribute 'comboslot'

Is there a way to do it via code likes this ?
You would need to show more code so we can make any sense of it, but yes you can add items to a comboBox with a for loop.. You can also simply add multiples items at once by passing a list into the addItems() method.
(Dec-09-2018, 01:21 AM)shift838 Wrote: [ -> ]Is there a way to use a for loop to add items to comboboxes. All items would be the same in each combobox.

I thought the below might work but nada:

x equals the combobox #, all have the same name but just increase in numbers. Comboboxes are named 'comboboxslot2' to 'comboboxslot8'.

I want to add the same items via a loop based on the selection that is made in the 'comborom' object.

#!/usr/bin/python3
import os
import sys
from PyQt5 import QtCore, QtGui, QtWidgets, uic

LOCAL_DIR = os.path.dirname(os.path.realpath(__file__)) + "/"


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.ui = uic.loadUi(LOCAL_DIR + "ooeygui.ui", self)
        self.ui.comborom.currentTextChanged.connect(self._pullcomboromText)
        self.ui.checkboxsb.stateChanged.connect(self._pullcheckboxsb)
        self.ui.buttonlaunch.clicked.connect(self._buttonLaunch)
        self.ui.checkBoxhexbusfloppy.stateChanged.connect(self._pullcheckboxhbfloppy)
        self.ui.checkBoxhexbusrs232.stateChanged.connect(self._pullcheckboxhbrs232)

        self.show()

    def _pullcomboromText(self, currentText):
        global sr
        if currentText == "Myarc Geneve 9640" or currentText == "Myarc Geneve 9640 (GenMod)":
            for x in range(2,9):
            self.ui.comboboxslot(x).addItem("item1")
            self.ui.comboboxslot(x).addItem("item2")
            self.ui.comboboxslot(x).addItem("item3")
            self.ui.comboboxslot(x).addItem("item4")
            self.ui.comboboxslot(x).addItem("item5")
            self.ui.comboboxslot(x).addItem("item6")
            self.ui.comboboxslot(x).addItem("item7")
            

if __name__ == '__main__':
    app = QtWidgets.QApplication([])
    gui = Main()
    sys.exit(app.exec_())
I get an error stating: AttributeError: 'Main' object has no attribute 'comboslot'

Is there a way to do it via code likes this ?

I have updated the rest to include the complete code.
self.ui = uic.loadUi(LOCAL_DIR + "ooeygui.ui", self)

maybe you can use

import ooeygui.ui


and I think you nedd to indent

    def _pullcomboromText(self, currentText):
        global sr
        if currentText == "Myarc Geneve 9640" or currentText == "Myarc Geneve 9640 (GenMod)":
            for x in range(2,9):
                self.ui.comboboxslot(x).addItem("item1")
                self.ui.comboboxslot(x).addItem("item2")
                self.ui.comboboxslot(x).addItem("item3")
                self.ui.comboboxslot(x).addItem("item4")
                self.ui.comboboxslot(x).addItem("item5")
                self.ui.comboboxslot(x).addItem("item6")
                self.ui.comboboxslot(x).addItem("item7")
We can't tell what the self.ui.comboboxslot(x) are referring to, but supposing that you have comboboxslot_1, comboboxslot_2 and so on in your UI file, and that you would want to add the same item to each in a loop, you can do it like this:

combos = [self.ui.comboboxslot_1, self.ui.comboboxslot_2]
for c in combos:
	c.addItem("item1")

Another way would be to init them from your program and put them in a dict that you can call later.

self.combos = {}
self.ui.layout = QtWidgets.QVBoxLayout()
for x in range(2, 9):
	self.combos[x] = QtWidgets.QComboBox()
	self.ui.layout.addWidget(self.combos[x])
...

for c in self.combos:
	self.combos[c].addItem("item1")
(Dec-09-2018, 07:46 PM)Alfalfa Wrote: [ -> ]We can't tell what the self.ui.comboboxslot(x) are referring to, but supposing that you have comboboxslot_1, comboboxslot_2 and so on in your UI file, and that you would want to add the same item to each in a loop, you can do it like this:

combos = [self.ui.comboboxslot_1, self.ui.comboboxslot_2]
for c in combos:
	c.addItem("item1")

Another way would be to init them from your program and put them in a dict that you can call later.

self.combos = {}
self.ui.layout = QtWidgets.QVBoxLayout()
for x in range(2, 9):
	self.combos[x] = QtWidgets.QComboBox()
	self.ui.layout.addWidget(self.combos[x])
...

for c in self.combos:
	self.combos[c].addItem("item1")

Excellent! works great! thank you!