skip to Main Content

Good Day

I’m stuck with some code. I need to do two things: 1. Create multiple checkboxes and add the ticked boxes to a list. 2. Add multiple tuples/lists to MySQL.

Checkboxes (My code so far):

def weapon_submit():
    selection_list = ['Blueprint']
    ticked_items = weapon_selection.get()
    for btn in check_buttons:
        if ticked_items == 1:
            item_name = weapon_box.cget('text')
            selection_list += item_name

    # Checkboxes
    current_row = 1
    current_col = 0
    check_buttons = []
    for item in weapon_part_list:
        weapon_selection = StringVar()
        weapon_box = Checkbutton(weapon_window, text=item, variable=weapon_selection)
        weapon_box.config(bg=bg_color, fg=font_color, font=text_font, selectcolor=blue)
        weapon_box.deselect()
        weapon_box.grid(row=current_row, column=current_col, padx=15, pady=15)
        check_buttons.append(weapon_selection)
        current_row += 1
        if current_row == 4:
            current_row = 1
            current_col += 1

    # Button
    enter_button = Button(weapon_window, text="Submit", command=weapon_submit, bg=btn_color, fg=font_color, font=text_font, relief=GROOVE)
    enter_button.grid(row=5, column=2, pady=(30, 0))

Just an update if people are searching for a solution to above problem, with help from another user’s post I figured it out. See below:

def weapon_submit():
selection_list = ['Blueprint']
for text, weapon_selection in zip(weapon_part_list, check_buttons):
    if weapon_selection.get():
        ticked_items = text
        selection_list.append(ticked_items)

Database (My code so far):

warframe_part_list = [
        'Blueprint',
        'Neuroptics',
        'Chassis',
        'Systems'
    ]

def number_valid(P):
    if str.isdigit(P) or str(P) == "":
          return True
    else:
          messagebox.showwarning(parent=relics_window, message="Only numbers allowed!")
      return False

vcmd = add_relics_window.register(number_valid)
prime_entry = Entry(add_relics_window, width=25, justify=CENTER, font=text_font, bg=label_color, fg=label_font_color)
prime_entry.grid(row=2, column=1, padx=15)

  
current_row = 5
for part in warframe_part_list:
            part_label = Label(add_relics_window, text=part, font=heading_font, bg=bg_color, fg=font_color)
            part_label.grid(row=current_row, column=0)

clicked_relic = StringVar()
clicked_relic.set("Select Option")
relic_menu = OptionMenu(add_relics_window, clicked_relic, *relic_list)
relic_menu.config(bg=btn_color, fg=font_color, font=text_font)
relic_menu.grid(row=current_row, column=1, padx=(15, 0))

relic_no_entry = Entry(add_relics_window, width=5, justify=CENTER, font=text_font, bg=label_color, fg=label_font_color)
relic_no_entry.grid(row=current_row, column=2)

clicked_rarity = StringVar()
clicked_rarity.set("Select Option")
rarity_menu = OptionMenu(add_relics_window, clicked_rarity, *rarity_list)
rarity_menu.config(bg=btn_color, fg=font_color, font=text_font)
rarity_menu.grid(row=current_row, column=3)

quantity_entry = Entry(add_relics_window, width=5, validate='key', validatecommand=(vcmd, '%P'), justify=CENTER, font=text_font, bg=label_color, fg=label_font_color)
quantity_entry.grid(row=current_row, column=4)

clicked_mission = StringVar()
clicked_mission.set("Select Option")
mission_menu = OptionMenu(add_relics_window, clicked_mission, *mission_list)
mission_menu.config(bg=btn_color, fg=font_color, font=text_font)
mission_menu.grid(row=current_row, column=5)

clicked_planet = StringVar()
clicked_planet.set("Select Option")
planet_menu = OptionMenu(add_relics_window, clicked_planet, *planet_list)
planet_menu.config(bg=btn_color, fg=font_color, font=text_font)
planet_menu.grid(row=current_row, column=6, padx=(15, 0))

location_entry = Entry(add_relics_window, width=25, justify=CENTER, font=text_font, bg=label_color, fg=label_font_color)
location_entry.grid(row=current_row, column=7, padx=15)

clicked_rotation = StringVar()
clicked_rotation.set("Select Option")
rotation_menu = OptionMenu(add_relics_window, clicked_rotation, *rotation_list)
rotation_menu.config(bg=btn_color, fg=font_color, font=text_font)
rotation_menu.grid(row=current_row, column=8)

drop_chance_entry = Entry(add_relics_window, width=10, justify=CENTER, font=text_font, bg=label_color, fg=label_font_color)
drop_chance_entry.grid(row=current_row, column=9)

clicked_obtained = StringVar()
clicked_obtained.set("Select Option")
obtained_menu = OptionMenu(add_relics_window, clicked_obtained, *answer_list)
obtained_menu.config(bg=btn_color, fg=font_color, font=text_font)
obtained_menu.grid(row=current_row, column=10)

current_row += 1

enter_button = Button(add_relics_window, text="Submit", command=submit, bg=btn_color, fg=font_color, font=text_font, relief=GROOVE)
enter_button.grid(row=10, column=7, pady=(30, 0))
    
def submit():
    data_list = []
    for part in warframe_part_list:
            list = (prime_entry.get(), part, clicked_relic.get(), relic_no_entry.get(), clicked_rarity.get(), quantity_entry.get(), clicked_mission.get(), clicked_planet.get(), location_entry.get(), clicked_rotation.get(), drop_chance_entry.get(), clicked_obtained.get())
             data_list.append(list)

connections = mysql.connector.connect(user=user, password=password, host=host, database=database)
cursors = connections.cursor()
sql = "INSERT INTO relics (prime, part, relic, relic_no, rarity, quantity, drop_mission, drop_planet, drop_location, drop_rotation, drop_chance, obtained) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
cursors.executemany(sql, data_list)
connections.commit()
connections.close()

Database output should look like this:

  1. Blueprint, Lith, P11, Rare, 5, Defense, Void
  2. Neuroptics, Meso, R4, Common, 3, Capture, Void
  3. Chassis, Neo, S17, Uncommon, 1, Survival, Void
  4. Systems, Axi, K6, Uncommon, 0, Survival, Void

Loop is replacing each sublist instead of adding them and I end up with the last list 4 times in the main list.

You’re help is much appreciated.

ps. How do I shorten below code to add all list simultaneously in a single loop. eg.

for x in companion_type_list:
            sql = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
            data = (x, 'Sentinel', 0, 'No', 0, "None")
            cursors.execute(sql, data)

Looping through all sub lists?

 # $$$$  Add All Companions  $$$$$
    companion_type_list = [
        'Sentinels',
        'MOA',
        'Hound',
        'Kubrow',
        'Kavat',
        'Predasite',
        'Vulpaphyla']
    
    sentinel_list = ['Carrier', 'Dethcube', 'Diriga', 'Djinn', 'Helios', 'Nautilus', 'Oxylus', 'Shade', 'Taxon', 'Wyrm']
    moa_list = ['Lambeo', 'Nychus', 'Oloro', 'Para']
    hound_list = ['Bhaira', 'Dorma', 'Hec']
    kubrow_list = ['Chesa', 'Charger', 'Huras', 'Raksa', 'Sahasa', 'Sunika']
    kavat_list = ['Adarza', 'Smeeta', 'Vasca']
    predasite_list = ['Medjay', 'Pharaoh', 'Vizier']
    vulpaphyla_list = ['Crescent', 'Panzer', 'Sly']

    connections = mysql.connector.connect(user=user, password=password, host=host, database=database)
    cursors = connections.cursor()
    cursors.execute('SELECT id FROM companions_list')
    records = cursors.fetchone()
    if records is None:
        for x in sentinel_list:
            sql = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
            data = (x, 'Sentinel', 0, 'No', 0, "None")
            cursors.execute(sql, data)
        for x in moa_list:
            sql = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
            data = (x, 'Moa', 0, 'No', 0, "None")
            cursors.execute(sql, data)
        for x in hound_list:
            sql = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
            data = (x, 'Hound', 0, 'No', 0, "None")
            cursors.execute(sql, data)
        for x in kubrow_list:
            sql = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
            data = (x, 'Kubrow', 0, 'No', 0, "None")
            cursors.execute(sql, data)
        for x in kavat_list:
            sql = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
            data = (x, 'Kavat', 0, 'No', 0, "None")
            cursors.execute(sql, data)
        for x in predasite_list:
            sql = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
            data = (x, 'Predasite', 0, 'No', 0, "None")
            cursors.execute(sql, data)
        for x in vulpaphyla_list:
            sql = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
            data = (x, 'Vulpaphyla', 0, 'No', 0, "None")
            cursors.execute(sql, data)
        connections.commit()
        connections.close()

2

Answers


  1. Note: This is the shortened code, not the fix.

    # $$$$  Add All Companions  $$$$$
        companion_type_list = [
            'Sentinels',
            'MOA',
            'Hound',
            'Kubrow',
            'Kavat',
            'Predasite',
            'Vulpaphyla']
        
        sentinel_list = ['Carrier', 'Dethcube', 'Diriga', 'Djinn', 'Helios', 'Nautilus', 'Oxylus', 'Shade', 'Taxon', 'Wyrm']
        moa_list = ['Lambeo', 'Nychus', 'Oloro', 'Para']
        hound_list = ['Bhaira', 'Dorma', 'Hec']
        kubrow_list = ['Chesa', 'Charger', 'Huras', 'Raksa', 'Sahasa', 'Sunika']
        kavat_list = ['Adarza', 'Smeeta', 'Vasca']
        predasite_list = ['Medjay', 'Pharaoh', 'Vizier']
        vulpaphyla_list = ['Crescent', 'Panzer', 'Sly']
    
        connections = mysql.connector.connect(user=user, password=password, host=host, database=database)
        cursors = connections.cursor()
        cursors.execute('SELECT id FROM companions_list')
        records = cursors.fetchone()
        SQL = "INSERT INTO companions_list (companion, type, my_rank, reactor, forma, status) VALUES (%s, %s, %s, %s, %s, %s)"
        if records is None:
            for x in sentinel_list:
                data = (x, 'Sentinel', 0, 'No', 0, "None")
                cursors.execute(SQL, data)
            for x in moa_list:
                data = (x, 'Moa', 0, 'No', 0, "None")
                cursors.execute(SQL, data)
            for x in hound_list:
                data = (x, 'Hound', 0, 'No', 0, "None")
                cursors.execute(SQL, data)
            for x in kubrow_list:
                data = (x, 'Kubrow', 0, 'No', 0, "None")
                cursors.execute(SQL, data)
            for x in kavat_list:
                data = (x, 'Kavat', 0, 'No', 0, "None")
                cursors.execute(SQL, data)
            for x in predasite_list:
                data = (x, 'Predasite', 0, 'No', 0, "None")
                cursors.execute(SQL, data)
            for x in vulpaphyla_list:
                data = (x, 'Vulpaphyla', 0, 'No', 0, "None")
                cursors.execute(SQL, data)
            connections.commit()
            connections.close()
    
    Login or Signup to reply.
  2. Since you have used same set of variables in the for loop, so those set of variables will reference the last row of widgets after the for loop.

    Suggest to use a dictionary to store those widgets in a row using part as the key:

    rows_of_widgets = {}  # dictionary to store the widgets in each row
    current_row = 5
    for part in warframe_part_list:
        # create those widgets as in the posted code
        ...
        # save current row of widgets into the dictionary
        rows_of_widgets[part] = [clicked_relic, relic_no_entry,
                                 clicked_rarity, quantity_entry,
                                 clicked_mission, clicked_planet,
                                 location_entry, clicked_rotation,
                                 drop_chance_entry, clicked_obtained]
    
        current_row += 1
    

    Then modify submit() to loop through the dictionary:

    def submit():
        data_list = []
        for part in warframe_part_list:
            values = [prime_entry.get(), part] + [item.get() for item in rows_of_widgets[part]]
            data_list.append(values)
    
        # insert data_list into database table
        ...
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search