Python – 在重启之间保存程序的状态?

我想知道如何保存程序当前设置,以便它在程序重启或计算机重启时除非另有说明,否则保持不变.例如,Windows默认程序便签,它保存文本,以便即使在计算机关闭后也可以使用它.

是否有某种模块可以导入?我的程序基本上是一个任务列表程序,您可以在其中添加内容,并使用wxPython复选框将其勾选.即使在程序退出后,还有任何可能的方法来保持它的状态吗?

如果有人能用我的代码向我展示一个例子,我们将不胜感激,不要担心,我不会简单地复制它并完成它.它将被视为我的学习经历,以便我将来可以使用它.谢谢.

这是我的计划:

import wx, sys,os

mylist = []

class test(wx.Frame):

def __init__(self, parent, id):


    self.count = 1
    #Frame
    wx.Frame.__init__(self,parent,id,'List',size = (200,500))
    #Panel
    self.panel = wx.Panel(self)
    item = wx.TextEntryDialog(None, "List Title")
    if item.ShowModal() == wx.ID_OK:
        print 'here'
        answer = item.GetValue()
        mylist.append(answer)
        print mylist
        windtitle = wx.StaticText(self.panel, -1, answer, (10,10))
        windtitle.SetForegroundColour("blue")


    addButton = wx.Button(self.panel, label = "+ Add", pos=(40,450), size = (60,-1))
    finishButton = wx.Button(self.panel, label = "Finish", pos=(110,450), size = (60,-1))

    self.Bind(wx.EVT_BUTTON, self.addtomenu, addButton)
    self.Bind(wx.EVT_BUTTON, self.finish, finishButton)

def finish(self, event):
    self.Destroy()
    sys.exit()

def addtomenu(self,event):

    newitem = wx.TextEntryDialog(None, "New Item")
    if newitem.ShowModal() == wx.ID_OK:
        count = len(mylist)+1
        print count
        yaxis = 20*count
        if count == 21:
            wx.StaticText(self.panel, -1, "List To Full", (10, yaxis))
        else:
            answer = newitem.GetValue()
            mylist.append(answer)
            print mylist
            self.Bind(wx.EVT_CLOSE, self.closewindow)
            wx.CheckBox(self.panel, -1, answer, (10,yaxis), size = (200,-1)) 



def closewindow(self, event):
    self.Destroy()






if __name__ == "__main__":
    app=wx.PySimpleApp()  #Blood
    frame = test(parent=None, id = -1)  #Skin
    frame.Show()
    app.MainLoop()  #Heart

解决方法:

以下是如何在JSON文件中保存程序状态的示例.您已经有一个finish方法,在程序退出或Finish按钮关闭时调用.我们现在可以使用它来调用save方法,该方法将状态保存到JSON文件.

def finish(self, event):
    self.save()
    self.Destroy()
    sys.exit()

def save(self):
    windtitle = self.windtitle.GetLabelText()
    checkboxes = [{'checked': child.IsChecked(),
                   'label': child.GetLabel()}
                  for child in self.panel.GetChildren()
                  if isinstance(child, wx.CheckBox)]
    data = {
        'windtitle':windtitle,
        'checkboxes':checkboxes,
        }
    with open(CONFIGFILE, 'w') as f:
        json.dump(data, f)

以下是如何读取JSON数据以重构GUI:

def load(self):
    if os.path.exists(CONFIGFILE):
        with open(CONFIGFILE, 'r') as f:
            data = json.load(f)
        title = data['windtitle']
        self.windtitle = wx.StaticText(self.panel, -1, title)
        self.vbox.Add(self.windtitle)
        for checkbox in data['checkboxes']:
            label = checkbox['label']
            cb = wx.CheckBox(
                self.panel, -1, checkbox['label'])
            self.vbox.Add(cb)                
            cb.SetValue(checkbox['checked'])
    else:
        self.create_windtitle()
    self.create_buttons()

例如:

import wx, sys, os
import json

CONFIGFILE = os.path.expanduser('~/tasklist.json')
class test(wx.Frame):
    def __init__(self, parent, id):
        frame = wx.Frame.__init__(self, parent, id, 'List', size = (200,500))
        self.panel = wx.Panel(self)
        self.panelbox = wx.BoxSizer(wx.VERTICAL)                

        self.vbox = wx.BoxSizer(wx.VERTICAL)        
        self.load()

        self.panelbox.Add(self.vbox)        
        self.panelbox.Add(self.buttonbox)

        self.panel.SetSizer(self.panelbox)
        self.panelbox.Fit(self)

        self.Bind(wx.EVT_BUTTON, self.addtomenu, self.addButton)
        self.Bind(wx.EVT_BUTTON, self.finish, self.finishButton)
        self.Bind(wx.EVT_CLOSE, self.finish)

    def create_buttons(self):
        self.buttonbox = wx.BoxSizer(wx.VERTICAL)                
        self.addButton = wx.Button(
            self.panel, label = "+ Add")
        self.finishButton = wx.Button(
            self.panel, label = "Finish")
        self.buttonbox.Add(self.addButton)
        self.buttonbox.Add(self.finishButton)

    def create_windtitle(self):
        item = wx.TextEntryDialog(None, "List Title")
        if item.ShowModal() == wx.ID_OK:
            answer = item.GetValue()
            self.windtitle = wx.StaticText(self.panel, -1, answer)
            self.windtitle.SetForegroundColour("blue")

    def addtomenu(self, event):
        newitem = wx.TextEntryDialog(None, "New Item")
        if newitem.ShowModal() == wx.ID_OK:
            if len(self.mylist) > 5:
                wx.StaticText(self.panel, -1, "List To Full")
            else:
                answer = newitem.GetValue()
                cb = wx.CheckBox(self.panel, -1, answer)
                self.vbox.Add(cb)
        self.panelbox.Fit(self)

    def finish(self, event):
        self.save()
        self.Destroy()
        sys.exit()

    @property
    def mylist(self):
        return [ child.GetLabel()
                 for child in self.panel.GetChildren()
                 if isinstance(child, wx.CheckBox) ]

    def save(self):
        windtitle = self.windtitle.GetLabelText()
        checkboxes = [{'checked': child.IsChecked(),
                       'label': child.GetLabel()}
                      for child in self.panel.GetChildren()
                      if isinstance(child, wx.CheckBox)]
        data = {
            'windtitle':windtitle,
            'checkboxes':checkboxes,
            }
        with open(CONFIGFILE, 'w') as f:
            json.dump(data, f)

    def load(self):
        if os.path.exists(CONFIGFILE):
            with open(CONFIGFILE, 'r') as f:
                data = json.load(f)
            title = data['windtitle']
            self.windtitle = wx.StaticText(self.panel, -1, title)
            self.vbox.Add(self.windtitle)
            for checkbox in data['checkboxes']:
                label = checkbox['label']
                cb = wx.CheckBox(
                    self.panel, -1, checkbox['label'])
                self.vbox.Add(cb)                
                cb.SetValue(checkbox['checked'])
        else:
            self.create_windtitle()
        self.create_buttons()

if __name__ == "__main__":
    app = wx.PySimpleApp()  #Blood
    frame = test(parent = None, id = -1)  #Skin
    frame.Show()
    app.MainLoop()  #Heart

顺便说一句,不要使用显式位置在GUI中放置小部件.那条路导致疯狂.如果使用位置(例如pos =(10,yaxis)),随着GUI的增长,修改布局变得越来越困难.每个元素的位置都依赖于其他元素的位置,很快就会变得无法管理.

每个GUI框架都提供了一些更好的方法来实现漂亮的布局.我对wxpython不太熟悉,但它似乎使用了BoxSizers.我上面使用的布局非常简陋.我确信通过对wxpython布局设计模式的一些研究可以实现更好的布局.

有时我需要找出小部件的所有属性和方法.例如,我不知道如何向Panel询问它包含哪些Checkbox.我发现它使用这个功能:

def describe(obj):
    for key in dir(obj):
        try:
            val = getattr(obj, key)
        except AttributeError:
            continue
        if callable(val):
            help(val)
        else:
            print('{k} => {v}'.format(k = key, v = val))
        print('-'*80)

describe(self.panel)

这是我在utils_debug中的功能.

上一篇:docker常用命令


下一篇:Docker下使用disconf:极速体验