How to change an item in a list forever?

69 views Asked by At

I have a program in tkinter which I have a list in it and I transferred the list items to a list box. when I run it, there is a list box and you're able to change the items by pressing buttons and writing something in the Entry box. here is my code

import tkinter as tk
from tkinter import ttk, ACTIVE, END
window = tk.Tk()
window.title("برنامهء مغازه")
window.geometry("900x500")
window.configure(background = "white")
window.resizable(width=False,height=False)

tab_parent = ttk.Notebook(window)
tab_1 = ttk.Frame(tab_parent)
tab_2 = ttk.Frame(tab_parent)
tab_parent.add(tab_1,text="خرید قسطی")
tab_parent.add(tab_2,text="کالاها")

def Update(data):
    list_box.delete(0,END)
    for item in data:
    list_box.insert(END,item)

def fillout(e):
    entry_search.delete(0,END)
    entry_search.insert(0,list_box.get(ACTIVE))

def check(e):
    typed = entry_search.get()
    if typed == "":
        data = products
    else:
        data = []
        for item in products:
            if typed in item:
                data.append(item)
    Update(data)

label_search = tk.Label(tab_2,text="جستجو")
label_search.grid(column=0,row=0)
label_search.pack()
entry_search = tk.Entry(tab_2,justify="right",width="60")
entry_search.pack(pady=5)
list_box = tk.Listbox(tab_2,justify="right",width="60")
list_box.pack(pady=15)

products = ["مخلوط کن دلمونتی",
"هم زن حرفه ای دلمونتی ",
"غذا ساز 4 کاره دلمونتی",
"آب مرکبات گیر دلمونتی"]
def edit():
    typed_2 = entry_search.get()
    if typed_2 == "":
    data = products
    elif typed_2 in products:
    global index_item_in_products
    index_item_in_products = products.index(typed_2)

def confirm():
    typed_3 = entry_search.get()
    products[index_item_in_products] = typed_3
    Update(products)

btn_edit = tk.Button(tab_2,text="ویرایش",command=edit)
btn_edit.pack(pady=5)
btn_edit_confirm = tk.Button(tab_2,text="تایید ویرایش",command=confirm)
btn_edit_confirm.pack(pady=10)

Update(products)
list_box.bind("<<ListboxSelect>>",fillout)
entry_search.bind('<KeyRelease>',check)

tab_parent.pack()
window.mainloop()

But the problem is that when I rerun the code after changing an item ,the items are just the previous ones. I expect that when I change an item, it will be saved in the memory(storing it) and when I rerun the program, the item is the changed one. what should I do?

1

There are 1 answers

0
Michael On

So the thing here is that your running the same code with the same hard coded values. You will have to store the list separately (in a different file). A common beginner friendly way to do this is with json. Take a look at this https://www.w3schools.com/python/python_json.asp

Edit: When the file starts up you can run a function such as

def load_data(file_name):
    with open(file_name, 'r') as file:
        data = json.load(file)
    return data

you can then use the access the data like this data = load_data(json_file)

to then save the data when you have made changes

def save_data(data, file_name):
    with open(file_name, 'w') as file:
        json.dump(data, file, indent=4)

This will then be saves in the the same json file ready for when you start the file again.