Python Forum
[PyQt] Help Iterating through to read comboBox Selections
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] Help Iterating through to read comboBox Selections
#1
Hello,

I have 22 ComboBoxes where the user can choose different values for each one.
The problem I'm having is that I don't know how to iterate through every single comboBox to see if there is a valid selection (a valid selection is anything but '-').
I tried if isinstance but it only reads the last comboBox and not the rest.

How do I make it, so it iterates through all 22 comboBoxes and stores the currentText() so I can use it to retrieve info (Main_Category, Sell_Price_$) from another Table within my SQLite Database.

Snippet:
#----------------------------------------------------------------------------------------------------
#                                      Button Actions
#----------------------------------------------------------------------------------------------------
        #------------------------------------------
                        #Submit Button
        #------------------------------------------
        #When the Submit button is clicked -> SubmitClicked Function
        SubmitButton = self.SubmitButton
        SubmitButton.clicked.connect(self.SubmitClicked)
        #------------------------------------------

#----------------------------------------------------------------------------------------------------
#                               Calculations
#----------------------------------------------------------------------------------------------------

        #------------------------------------------
        #  Subtract Material Used from Inventory
        #------------------------------------------
        #TODO:
        #Grab Main_Category where Name = Material_1-22_ComboBox.currentText()
        #If Main_Category is Wire: subtract MaterialQuantity from the Total_Length_Ft Column &
        #populate the MaterialPrice SpinBox with the Price_Per_Ft
        #Else, subtract the MaterialQuanity from the inventory Quantity &
        #populate the MaterialPrice SpinBox with the Sell_Price_$
        #Apply quantity changes once Submit Button is clicked

        #Make Database table in Inventory.db so it's easier to work with
        #------------------------------------------
    def SubmitClicked(self):

        for widget in self.MaterialUsedFrame.children():
            if isinstance(widget, QComboBox):
                SelectedItemName = widget.currentText()

        #Connect to the inventory database (inventory.db)
        connection = sqlite3.connect(InventoryDatabase)
        cursor = connection.cursor()
        cursor.execute("SELECT Main_Category FROM items WHERE Name = ?",(SelectedItemName,))
        connection.commit()
        Result = cursor.fetchone()
        #Close the connection
        connection.close()
        print(Result)

        if Result == ('Wire',):
            #Connect to the inventory database (inventory.db)
            connection = sqlite3.connect(InventoryDatabase)
            cursor = connection.cursor()
            cursor.execute("SELECT Total_Length_Ft, Price_Per_Ft FROM items WHERE Name = ?",(SelectedItemName,))
            connection.commit()
            Result = cursor.fetchone()
            #Close the connection
            connection.close()
            print(Result)

        else: 
            #Connect to the inventory database (inventory.db)
            connection = sqlite3.connect(InventoryDatabase)
            cursor = connection.cursor()
            cursor.execute("SELECT Quantity, Sell_Price_$ FROM items WHERE Name = ?",(SelectedItemName,))
            connection.commit()
            Result = cursor.fetchone()
            #Close the connection
            connection.close()
            print(Result)
#----------------------------------------------------------------------------------------------------
Thanks in advance.

Attached Files

Thumbnail(s)
   
Reply
#2
Your logic is all wrong. You should check each combo box and stop if any of them have an invalid selection.

Using .children() should be avoided for all but the most generic operations (like update). Your code quickly becomes complicated if you have combo boxes that need to be treated differently. But if you want to continue with such a poorly thought out practice, at least do it right.
for widget in self.MaterialUsedFrame.findChildren(QComboBox):
    if widget.currentText() == "-":
        # Somehow indicate that there is a bad selection in one of the combo boxes?
        break  # Found a bad selection.  Stop looking
else:
    # All the combo box selections are valid
Or you could use any()
if any((cbox.currentText() == "-" for cbox in self.MaterialUsedFrame.findChildren(QComboBox))):
I still advocate for keeping your 22 combo boxes in a list and using a descriptive name.
if any((cbox.currentText() == "-" for cbox in self.material_selections):
Reply
#3
So I managed to get it to print in the terminal (for testing) the way I want it to:
Output:
14/2 BX 12/2 Romex (White) 14/3 BX - - - - - - - - - - - - - - - - - - - #(These are the blank/default placeholder values) None #(This is the SQL Results from the last ComboBox) None #(This is the SQL Results from the last ComboBox)
But I don't know how to grab the Categories, and Length, & Price from those items printed in the terminal (14/2, 12/2, & 14/3).

I would like it to be like this:
Output:
14/2 BX ('Wire', 100, 0.15) #->(Category,Length,Price) for 14/2 BX 12/2 Romex (White) ('Wire', 25, 0.15) 14/3 BX ('Wire', 200, 0.15) - - - - - - - - - - - - - - - - - - -
So is there a way to iterate through that and just grab/store the Category, Length, Price for each item Name (14/2 BX, 12/2 Romex (White), 14/3 BX)
and ignore '-'?

Attached Files

Thumbnail(s)
   
Reply
#4
all_ok = True
for widget in self.MaterialUsedFrame.findChildren(QComboBox):
    if widget.currentText() == "-":
        # Do whatever you do when selection is bad
        all_ok = False
    else:
        # Do whatever you do when selection is good
Looking at your panel I would combine the quantity, material and price into a single widget. The widget would know that changing quantity or material should update the price. Another idea is to replace all the widgets with a table.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] How can I sync Combobox index to other combobox index? nickzsche 2 2,399 Jan-03-2022, 12:29 PM
Last Post: Axel_Erfurt
  [Tkinter] eport selections to dic mintsilver 0 2,372 Jan-16-2017, 01:06 PM
Last Post: mintsilver

Forum Jump:

User Panel Messages

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