Python Forum

Full Version: how do i save my all my edits performed in adminpanel with one button
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
first of all i am new to tkinter i tried to understand as much as i could and tried making the program accordingly now here is my current status
i have 3 files -userfile,json(database) and admin file 
i am working on admin file right now which is suppose to help me make changes performed in admin file directly to the json file 
i have created a finished button in which i need to add a function that would save all the changes made in the admin panel file directly to the database json file but idk how exactly to do that which function and how will it exactly work with that function 

ps- again i am new to tkinter so please bare me help would be great 


here is the link to my repository can anyone help me out ?

https://github.com/rashi-kp/tkinterquiz
Quote:
def finished():
    new_list = []
    for i in range(len(user_data)):
        new_list.append({}) # add a empty dictionary to the new list
        new_list[i]['answer'] = user_data[i]['answer'].get()
        # add question data and options data the same way

    #save the new data
    with open('db.json', 'w') as f:
        json.dump(new_list, f, indent=2)

Looks like you already do it.  ...except that you're saving the new file with a different name than what you're reading.  ie: everywhere else, you call it "database.json", but when you're writing changes, you create a new file called "db.json" instead.
(Apr-26-2017, 05:38 AM)nilamo Wrote: [ -> ]
Quote:
def finished():
    new_list = []
    for i in range(len(user_data)):
        new_list.append({}) # add a empty dictionary to the new list
        new_list[i]['answer'] = user_data[i]['answer'].get()
        # add question data and options data the same way

    #save the new data
    with open('db.json', 'w') as f:
        json.dump(new_list, f, indent=2)

Looks like you already do it.  ...except that you're saving the new file with a different name than what you're reading.  ie: everywhere else, you call it "database.json", but when you're writing changes, you create a new file called "db.json" instead.

yes i have changed the named to db.json because its corrupting the data store in json file called database.json so i have changed it to db.json to avoid that for time being
I'm confused then. The issue is that changes aren't getting saved back to the original json file. But you're purposefully not saving changes back to that file.
...so what's the problem?
(Apr-28-2017, 09:27 PM)nilamo Wrote: [ -> ]I'm confused then.  The issue is that changes aren't getting saved back to the original json file.  But you're purposefully not saving changes back to that file.
...so what's the problem?

just replace that code with this once and try running the file again u'll notice what i am exactly trying to tell

with open('database.json', 'w') as f:
        json.dump(new_list, f, indent=2)