鱼C论坛

 找回密码
 立即注册
查看: 1608|回复: 1

关于 python ini 配置

[复制链接]
发表于 2018-2-24 15:03:19 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本人想在程序中保存tkinter Listbox 的值,用pyinstaller 和ini文件打包后,不仅重新打开程序后没有保存,而且关闭时显示:Failed to execute script config
当然也有可能我的ini写的不对,敬请指导(附源码)

tkinter 源码(注释请省略)

  1. from tkinter import *


  2. root = Tk()
  3. root.title('简易分数系统(by:幻夜风暴工作室)')

  4. def ren():
  5.     lb1.insert(END,s1.get())



  6. def lb2():
  7.     lb2.insert(END, s1.get())

  8. def lb3():
  9.     lb3.insert(END, s1.get())

  10. def lb4():
  11.     lb4.insert(END, s1.get())

  12. def lb5():
  13.     lb5.insert(END, s1.get())

  14. def lb6():
  15.     lb6.insert(END, s1.get())

  16. def lb7():
  17.     lb7.insert(END, s1.get())

  18. def db1():
  19.     lb1.delete(ACTIVE)

  20. def db2():
  21.     lb2.delete(ACTIVE)

  22. def db3():
  23.     lb3.delete(ACTIVE)

  24. def db4():
  25.     lb4.delete(ACTIVE)

  26. def db5():
  27.     lb5.delete(ACTIVE)

  28. def db6():
  29.     lb6.delete(ACTIVE)

  30. def db7():
  31.     lb7.delete(ACTIVE)



  32. s1 = StringVar()


  33. e1 = Entry(root,width=15,highlightthickness=10,textvariable=s1,validate="key")
  34. b1 = Button(root,width=12,text='添加人名',command=ren)
  35. b2 = Label(root,text='添加分数')
  36. b3 = Button(root,text='语文',command=lb2)
  37. b4 = Button(root,text='数学',command=lb3)
  38. b5 = Button(root,text='英语',command=lb4)
  39. b6 = Button(root,text='生物地理',command=lb5)
  40. b7 = Button(root,text='道法历史',command=lb6)
  41. b8 = Button(root,text='音美体信',command=lb7)
  42. b9 = Button(root,text='人名',command=db1)
  43. b10 = Button(root,text='语文',command=db2)
  44. b11 = Button(root,text='数学',command=db3)
  45. b12 = Button(root,text='英语',command=db4)
  46. b13 = Button(root,text='生物地理',command=db5)
  47. b14 = Button(root,text='道法历史',command=db6)
  48. b15 = Button(root,text='音美体信',command=db7)




  49. e1.grid(row=0,column=0)
  50. b1.grid(row=1,column=0)
  51. b2.grid(row=0,column=1)
  52. b3.grid(row=0,column=2)
  53. b4.grid(row=0,column=3)
  54. b5.grid(row=0,column=4)
  55. b6.grid(row=0,column=5)
  56. b7.grid(row=0,column=6)
  57. b8.grid(row=0,column=7)
  58. b9.grid(row=2,column=1)
  59. b10.grid(row=3,column=1)
  60. b11.grid(row=4,column=1)
  61. b12.grid(row=5,column=1)
  62. b13.grid(row=6,column=1)
  63. b14.grid(row=7,column=1)
  64. b15.grid(row=8,column=1)


  65. l1 = Label(root,text='语文')
  66. l2 = Label(root,text='数学')
  67. l3 = Label(root,text='英语')
  68. l4 = Label(root,text='生物地理')
  69. l5 = Label(root,text='道法历史')
  70. l6 = Label(root,text='音美体信')
  71. l7 = Label(root,text='删除')
  72. l1.grid(row=1,column=2)
  73. l2.grid(row=1,column=3)
  74. l3.grid(row=1,column=4)
  75. l4.grid(row=1,column=5)
  76. l5.grid(row=1,column=6)
  77. l6.grid(row=1,column=7)
  78. l7.grid(row=1,column=1)

  79. lb1 = Listbox(root,width=15,height=20)#yscrollcommand=sb.set
  80. lb2 = Listbox(root,width=8,height=20)
  81. lb3 = Listbox(root,width=8,height=20)
  82. lb4 = Listbox(root,width=8,height=20)
  83. lb5 = Listbox(root,width=8,height=20)
  84. lb6 = Listbox(root,width=8,height=20)
  85. lb7 = Listbox(root,width=8,height=20)

  86. lb1.grid(column=0,row=2,rowspan=7)#,side=LEFT,fill=BOTH
  87. lb2.grid(column=2,row=2,rowspan=7)
  88. lb3.grid(column=3,row=2,rowspan=7)
  89. lb4.grid(column=4,row=2,rowspan=7)
  90. lb5.grid(column=5,row=2,rowspan=7)
  91. lb6.grid(column=6,row=2,rowspan=7)
  92. lb7.grid(column=7,row=2,rowspan=7)

  93. import configparser

  94. class Config:

  95.     def __init__(self,filepath):
  96.         '''初始化config对象'''

  97.         self.config = configparser.ConfigParser()
  98.         self.filepath = filepath

  99.     def __read(self,section,option):
  100.         '''读取节名为section,键名为option的值'''

  101.         self.config.readfp(open(self.filepath,'r'))
  102.         return self.config.get(section,option)

  103.     def read(self,section,option):

  104.         try:
  105.             return self.__read(section,option)
  106.         except Exception as e:
  107.             print(e)
  108.             return None

  109.     def write(self,section,option,value):
  110.         '''写入section下的option的值为value'''

  111.         self.config.readfp(open(self.filepath,'r'))
  112.         if self.config.has_section(section):
  113.             if self.config.has_option(section,option):
  114.                 if not self.read(section,option) == value:
  115.                     self.config.set(section,option,value)
  116.             else:
  117.                 self.config.set(section,option,value)
  118.         else:
  119.             self.config.add_section(section)
  120.             self.config.set(section,option,value)
  121.         
  122.         self.config.write(open(self.filepath,'w'))
  123.                

  124. if __name__ == "__main__":

  125.     x = Config('config.ini')
  126.     x.write("section1","key","value2")



  127. mainloop()
复制代码


ini 源码:
  1. [section]
  2. key = value

  3. [section1]
  4. key = value2
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2018-2-24 15:03:57 | 显示全部楼层
本人急用,谢谢
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-12-28 04:15

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表