I would like to know how to save a programs current settings so that it remains the same, unless specified otherwise, on program restart or computer restart. For example, windows default program sticky notes, where it saves the text, so that it can be used even after the computer is shut down.
Is there some sort of module that you can import? My program is basically a task list program, where you can add stuff to a list, and tick it off using wxPython check boxes. Is there any possible way to keep it’s state even after program exit?
It would be appreciated if someone could show me an example with my code, and don’t worry, I won’t just simply copy it and be done with it. It will be considered a learning experience for me, so that I may use it in the future. Thanks.
Here is my program:
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
Here is an example of how you might save the state of the program in a JSON file. You already have a
finishmethod which is called when the program exits or when the Finish button is closed. We can use it now to also call asavemethod which saves the state to a JSON file.And here is how you could read the JSON data to reconstitute the GUI:
For example:
By the way, do not use explicit positions for placing widgets in your GUI. That road leads to madness. If you use positions (e.g.
pos = (10,yaxis)), as your GUI grows, it becomes more and more difficult to modify your layout. Every element’s position becomes dependent on some other element’s position and it soon becomes unmanageable.Every GUI framework provides some saner way to achieve nice looking layouts. I’m not very familiar with
wxpython, but it seems to useBoxSizers. The layout I used above is very rudimentary. I’m sure a much nicer layout can be achieved with some study of wxpython layout design patterns.Sometimes I needed to find out what all the attributes and methods of a widget are. For example, I didn’t know how to ask a Panel what Checkboxes it contains. I found it using this function:
This is the function I had in utils_debug.