如何使用 configparser 更新值 python .ini 文件

How to update a value python .ini file with configparser

我正在编写一个代码,我可以将所有其他应用程序放入其中(如集线器)。我想将进度保存到 .ini 文件中。我使用了 .set 命令。我还在退出函数中添加了一个保存写入循环,但它不起作用。它显示此错误:

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()

请帮助我。也许有更好的方法。 (我英文不好所以可能会有拼写错误)

根据python documentation,代码的用法:

from configparser import ConfigParser

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

如果正在解析的文件有效,则正确。我建议输出数据并查看它是否正确解析:

from configparser import ConfigParser

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

输出应至少包含“DEFAULT”。如果没有,那么它没有正确解析,因此请确保给定的文件名有效。