0

I'm pretty embarassed, but i need to understand the sintax needed to build a for cycle to append 36 ComboBox text inputs into the same list. This is the code i used:


from PyQt5.QtWidgets import QApplication
from PyQt5.QtWidgets import QMainWindow


from PyQt5 import uic

class Ui(QMainWindow):
    def __init__(self):
        super().__init__()
        uic.loadUi("UI_psi.ui", self)

        self.avanti_psi.clicked.connect(self.sdq_page)
        self.avanti_psi.clicked.connect(self.psi_data)



    def sdq_page(self):
        uic.loadUi("UI_sdq.ui", self)
        self.Avanti_sdq.clicked.connect(self.indici_page)
    def indici_page(self):
        uic.loadUi("UI_indici.ui", self)
        print()
    def psi_data(self):
        psiData = []
        psiData.append(self.psi_1.currentText())
        psiData.append(self.psi_2.currentText())
        psiData.append(self.psi_3.currentText())

        print(psiData)







app = QApplication([])
window = Ui()

window.show()
app.exec()

I called the function "psi_data", and i feel really dumb adding all the ComboBox's current text as i did...but i can't figure out how to iterate every combobox in the UI_psi.ui and add em to the list psiData[]!

  • Please check this [post](https://stackoverflow.com/questions/7479915/getting-all-items-of-qcombobox-pyqt4-python) and let me know if that helped. – EnriqueBet Apr 02 '20 at 15:39
  • You'll have to find your own way of collecting that data. If they are correctly *named* (for example, their names all start with `psi_` and are correctly ordered), you can use something like `psiData = [getattr(self, 'psi_{}'.format(i)).currentText() for i in range(1, 37)]`. Be careful that you should not use `loadUi` more than once on the same instance, as it might give you unexpected results; use different windows or a `QStackedWidget` to have different interfaces while keeping the same window. – musicamante Apr 02 '20 at 15:48

1 Answers1

0

According to this post. I believe your psi_data function should look like this:

def psi_data(self):
    self.psiData = []
    self.psiData += [self.psi_1.itemText(i) for i in range(self.psi_1.count())]
    self.psiData += [self.psi_2.itemText(i) for i in range(self.psi_2.count())]
    self.psiData += [self.psi_3.itemText(i) for i in range(self.psi_3.count())]
Nimantha
  • 6,405
  • 6
  • 28
  • 69
EnriqueBet
  • 1,482
  • 2
  • 15
  • 23