Click here to Skip to main content
15,908,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have create an app for the shopper to record their shopping list, the add_list is a combobox where the added item will be displayed as a checkbutton in the shopping list, the data will be store in the grocery.pkl. Meanwhile the remove_list will remove a checked items from the shopping list and the grocery.pkl will update accordingly.

I got 2 questions:

When I click remove_list the following error appear check_box_list.remove(w) UnboundLocalError: local variable 'w' referenced before assignment

In my grocery.pkl I have saved 2 items: basil and beef. However display_list only shown a basil.

What I have tried:

Python
check_box_list = []
var = IntVar()

def remove_list():     
   for w in check_box_list: 
      if w.var.get() == 1:
        w.destroy() 
        check_box_list.remove(w)
        data = w.var.get()
   
   filename = "grocery.pkl"
   output_file = open(filename, 'wb') 
   pickle.dump(data, output_file)

def display_list():
   try:
       filename = "grocery.pkl"
       input_file = open(filename, 'rb') 
       data = pickle.load(input_file)
    
       var = IntVar()
       my_checkbutton = tb.Checkbutton(grocery_list, text=data, 
                                       variable=var,  bootstyle="danger")
       my_checkbutton.pack(pady=10)
       my_checkbutton.var = var
   except:
       alert(title='Error', text='No data in storage',  button='OK')
Posted
Updated 22-Jul-23 15:45pm
v3

1 solution

Your indentation is incorrect, so w only exists in the for loop. It should be:
Python
for w in check_box_list:
    if w.var.get() == 1:
        w.destroy()
        check_box_list.remove(w)    # I assume that these two lines
        data = w.var.get()          # belong to the preceding "if"

# do these lines belong inside or outside the for loop?
filename = "grocery.pkl"
output_file = open(filename, 'wb')
pickle.dump(data, output_file)
 
Share this answer
 
Comments
Member 15783420 22-Jul-23 10:25am    
Got it. How about my second question, the display function?
Richard MacCutchan 22-Jul-23 10:46am    
I have not used Checkbutton, but as far as I can see you only create a single instance. I would expect you need to loop through all the pickled items and display each one.
Member 15783420 22-Jul-23 21:10pm    
I have amended the code as above. However, whether the pickle line inside or outside the loop, the remove_list function will delete all the items. Why? What if I need to delete the checked items only?
Richard MacCutchan 23-Jul-23 3:25am    
Please use the Improve question link above, and show the actual code you are using. You also need to provide more information on the actual content of the pickle file, I cannot see what data you are saving.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900