[英]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.
我想将我的进度保存到一个.ini 文件中。 I used the.set command.
我使用了 .set 命令。 I also added a save write loop in the quit function, but it doesn't work.
我还在退出 function 中添加了一个保存写入循环,但它不起作用。 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保存文件.ini
[DEFAULT]
apps = blue
allapps = setings,powerbrowser
games = 0
allgames =
coins = 0
image = 0
toolbar_color = black
interface.py接口.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:根据python 文档,代码的用法:
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". output 至少应包含“DEFAULT”。 If it does not then, it's not parsing correctly, and so make sure that the filename given is valid.
如果没有,则说明解析不正确,因此请确保给定的文件名有效。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.