|
|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本人想在程序中保存tkinter Listbox 的值,用pyinstaller 和ini文件打包后,不仅重新打开程序后没有保存,而且关闭时显示:Failed to execute script config
当然也有可能我的ini写的不对,敬请指导(附源码)
tkinter 源码(注释请省略)
- from tkinter import *
- root = Tk()
- root.title('简易分数系统(by:幻夜风暴工作室)')
- def ren():
- lb1.insert(END,s1.get())
- def lb2():
- lb2.insert(END, s1.get())
- def lb3():
- lb3.insert(END, s1.get())
- def lb4():
- lb4.insert(END, s1.get())
- def lb5():
- lb5.insert(END, s1.get())
- def lb6():
- lb6.insert(END, s1.get())
- def lb7():
- lb7.insert(END, s1.get())
- def db1():
- lb1.delete(ACTIVE)
- def db2():
- lb2.delete(ACTIVE)
- def db3():
- lb3.delete(ACTIVE)
- def db4():
- lb4.delete(ACTIVE)
- def db5():
- lb5.delete(ACTIVE)
- def db6():
- lb6.delete(ACTIVE)
- def db7():
- lb7.delete(ACTIVE)
- s1 = StringVar()
- e1 = Entry(root,width=15,highlightthickness=10,textvariable=s1,validate="key")
- b1 = Button(root,width=12,text='添加人名',command=ren)
- b2 = Label(root,text='添加分数')
- b3 = Button(root,text='语文',command=lb2)
- b4 = Button(root,text='数学',command=lb3)
- b5 = Button(root,text='英语',command=lb4)
- b6 = Button(root,text='生物地理',command=lb5)
- b7 = Button(root,text='道法历史',command=lb6)
- b8 = Button(root,text='音美体信',command=lb7)
- b9 = Button(root,text='人名',command=db1)
- b10 = Button(root,text='语文',command=db2)
- b11 = Button(root,text='数学',command=db3)
- b12 = Button(root,text='英语',command=db4)
- b13 = Button(root,text='生物地理',command=db5)
- b14 = Button(root,text='道法历史',command=db6)
- b15 = Button(root,text='音美体信',command=db7)
- e1.grid(row=0,column=0)
- b1.grid(row=1,column=0)
- b2.grid(row=0,column=1)
- b3.grid(row=0,column=2)
- b4.grid(row=0,column=3)
- b5.grid(row=0,column=4)
- b6.grid(row=0,column=5)
- b7.grid(row=0,column=6)
- b8.grid(row=0,column=7)
- b9.grid(row=2,column=1)
- b10.grid(row=3,column=1)
- b11.grid(row=4,column=1)
- b12.grid(row=5,column=1)
- b13.grid(row=6,column=1)
- b14.grid(row=7,column=1)
- b15.grid(row=8,column=1)
- l1 = Label(root,text='语文')
- l2 = Label(root,text='数学')
- l3 = Label(root,text='英语')
- l4 = Label(root,text='生物地理')
- l5 = Label(root,text='道法历史')
- l6 = Label(root,text='音美体信')
- l7 = Label(root,text='删除')
- l1.grid(row=1,column=2)
- l2.grid(row=1,column=3)
- l3.grid(row=1,column=4)
- l4.grid(row=1,column=5)
- l5.grid(row=1,column=6)
- l6.grid(row=1,column=7)
- l7.grid(row=1,column=1)
- lb1 = Listbox(root,width=15,height=20)#yscrollcommand=sb.set
- lb2 = Listbox(root,width=8,height=20)
- lb3 = Listbox(root,width=8,height=20)
- lb4 = Listbox(root,width=8,height=20)
- lb5 = Listbox(root,width=8,height=20)
- lb6 = Listbox(root,width=8,height=20)
- lb7 = Listbox(root,width=8,height=20)
- lb1.grid(column=0,row=2,rowspan=7)#,side=LEFT,fill=BOTH
- lb2.grid(column=2,row=2,rowspan=7)
- lb3.grid(column=3,row=2,rowspan=7)
- lb4.grid(column=4,row=2,rowspan=7)
- lb5.grid(column=5,row=2,rowspan=7)
- lb6.grid(column=6,row=2,rowspan=7)
- lb7.grid(column=7,row=2,rowspan=7)
- import configparser
- class Config:
- def __init__(self,filepath):
- '''初始化config对象'''
- self.config = configparser.ConfigParser()
- self.filepath = filepath
- def __read(self,section,option):
- '''读取节名为section,键名为option的值'''
- self.config.readfp(open(self.filepath,'r'))
- return self.config.get(section,option)
- def read(self,section,option):
- try:
- return self.__read(section,option)
- except Exception as e:
- print(e)
- return None
- def write(self,section,option,value):
- '''写入section下的option的值为value'''
- self.config.readfp(open(self.filepath,'r'))
- if self.config.has_section(section):
- if self.config.has_option(section,option):
- if not self.read(section,option) == value:
- self.config.set(section,option,value)
- else:
- self.config.set(section,option,value)
- else:
- self.config.add_section(section)
- self.config.set(section,option,value)
-
- self.config.write(open(self.filepath,'w'))
-
- if __name__ == "__main__":
- x = Config('config.ini')
- x.write("section1","key","value2")
- mainloop()
复制代码
ini 源码:
- [section]
- key = value
- [section1]
- key = value2
复制代码 |
|