简体   繁体   中英

How to update a value python .ini file with configparser

I am writing a code where I can put all of my other apps in(like a hub). I want to save my progress into a.ini file. I used the.set command. I also added a save write loop in the quit function, but it doesn't work. it displays this error:

Traceback (most recent call last):
  File "C:\Users\User\Desktop\os\interface.py", line 20, in <module>
    apps = int(config['DEFAULT']['apps'])
  File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\configparser.py", line 1254, in __getitem__
    raise KeyError(key)
KeyError: 'apps'

savefile.ini

[DEFAULT]
apps = blue
allapps = setings,powerbrowser
games = 0
allgames = 
coins = 0
image = 0
toolbar_color = black

interface.py

from tkinter import *
from tkinter import messagebox
import datetime
import calendar
import os.path
from configparser import ConfigParser
import time

root = Tk()
root.title('Gamesim')
root.geometry('1870x1080')
root.attributes('-fullscreen', True)
config = ConfigParser()
config.read('savedata/savefile.ini')

now = datetime.datetime.now()
year = now.year
month = now.month
monthes = ['None', 'January', 'February', 'March', 'April', 'May', 'June', 'July', 'Augest', 'September', 'October', 'November', 'December']
apps = int(config['DEFAULT']['apps'])
allapps = config['DEFAULT']['allapps']
games = int(config['DEFAULT']['games'])
allgames = config['DEFAULT']['allgames']
coins = int(config['DEFAULT']['coins'])
time_now = f'{monthes[month]}, {year}'
background_image = ['bg/wallpaper0.png', 'bg/wallpaper1.png']
image = int(config['DEFAULT']['image'])
toolbar_color = config['DEFAULT']['toolbar_color']
ids = []



# Functions
def quit():
    global config, root
    MsgBox1 = messagebox.askquestion('Are you sure?', 'Are you sure you want to quit?',icon = 'warning')
    if MsgBox1 == 'yes':
        with open('savedata/savefile.ini', 'w') as configfile:
            config.write(configfile)
        root.destroy()

def startapp():
    global background
    background.forget()
def closeapp():
    global background
    background.pack(expand=True)


main = Frame(root)
xmain = -2
ymain = -2

# icons
shutdown_ico = PhotoImage(file='icons/shut_down.png')

backg = PhotoImage(file=background_image[image])
background = Label(main, image=backg)
tool = Frame(background, bg=toolbar_color, width=1870, height=50)
date = Label(tool, text=time_now, fg='white', bg=toolbar_color, font='Arial 15 bold')
shutdown = Button(tool, image=shutdown_ico, bg=toolbar_color, highlightthickness=0, relief=FLAT, command=quit)
border1 = Frame(tool, bg='grey', height=45, width=2)
yborder1 = 2

main.place(x=xmain, y=ymain)
tool.place(x=0, y=815)
background.pack(expand=True)
date.place(x=1370, y=10)
border1.place(x=60, y=yborder1)
shutdown.place(x=12, y=5)




root.mainloop()

Please help me. Maybe there is a better method. (Im bad in English so there might be spelling mistakes).

according to thepython documentation , usage of the code:

from configparser import ConfigParser

config = ConfigParser()
config.read('savedata/savefile.ini')
apps = int(config['DEFAULT']['apps'])

is correct if the file being parsed through is valid. I recommend outputting the data and seeing if it parses correctly:

from configparser import ConfigParser

config = ConfigParser()
config.read('savedata/savefile.ini')
print(config.sections())

the output should at least contain "DEFAULT". If it does not then, it's not parsing correctly, and so make sure that the filename given is valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM