Python Forum
pyqt5 combobox add item with loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
pyqt5 combobox add item with loop
#1
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 ?
SHIFT838
http://shift838.99er.net

Telnet BBS:
fusionbbs.ddns.net:9640
Reply
#2
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.
Reply
#3
(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.
SHIFT838
http://shift838.99er.net

Telnet BBS:
fusionbbs.ddns.net:9640
Reply
#4
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")
Reply
#5
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")
Reply
#6
(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!
SHIFT838
http://shift838.99er.net

Telnet BBS:
fusionbbs.ddns.net:9640
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] [Solved]Populate ComboBox with for loop? Extra 3 2,056 Jul-31-2022, 09:01 PM
Last Post: Extra
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,332 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt
Question [Tkinter] Can I set background color for each item in tkinter Combobox? water 1 5,057 Dec-10-2020, 07:48 PM
Last Post: Larz60+
  [PyQt] Control a combobox from another python file PyQt5 prath 0 2,242 May-05-2020, 03:22 PM
Last Post: prath
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 2,785 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [WxPython] How to show remove button at the right side of the hovering item of a combobox popup? indrajitmajumdar 0 2,492 Mar-28-2019, 11:24 AM
Last Post: indrajitmajumdar

Forum Jump:

User Panel Messages

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