簡體   English   中英

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

[英]How to update a value python .ini file with configparser

我正在編寫一個代碼,我可以將所有其他應用程序放入其中(如集線器)。 我想將我的進度保存到一個.ini 文件中。 我使用了 .set 命令。 我還在退出 function 中添加了一個保存寫入循環,但它不起作用。 它顯示此錯誤:

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'

保存文件.ini

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

接口.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 文檔,代碼的用法:

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

output 至少應包含“DEFAULT”。 如果沒有,則說明解析不正確,因此請確保給定的文件名有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM