有没有办法打开程序,点击几个togglebuttons,保存已切换的程序,然后如果再次打开它,你可以从文件读取并切换相同的按钮,这样你就可以继续或改变项目?
#!/usr/bin/python
# togglebuttons.py
import wx
toggle1=[]
toggle2=[]
toggle3=[]
toggle4=[]
toggle5=[]
toggle6=[]
class ToggleButtons(wx.Dialog):
def __init__(self, parent, id, title):
wx.Dialog.__init__(self, parent, id, title, size=(300, 300))
self.colour = wx.Colour(0, 0, 0)
wx.ToggleButton(self, 1, '1', (20, 25))
wx.ToggleButton(self, 2, '2', (20, 60))
wx.ToggleButton(self, 3, '3', (20, 100))
wx.ToggleButton(self, 4, '4', (20, 140))
wx.ToggleButton(self, 5, '5', (20, 180))
wx.ToggleButton(self, 6, '6', (20, 220))
self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleOne, id=1)
self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleTwo, id=2)
self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleThree, id=3)
self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleFour, id=4)
self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleFive, id=5)
self.Bind(wx.EVT_TOGGLEBUTTON, self.ToggleSix, id=6)
self.Centre()
self.ShowModal()
self.Destroy()
def ToggleOne(self,event):
if toggle1:
toggle1.remove()
else: toggle1.append(1)
def ToggleTwo(self,event):
if toggle2:
toggle2.remove()
else: toggle2.append(1)
def ToggleThree(self,event):
if toggle3:
toggle3.remove()
else: toggle3.append(1)
def ToggleFour(self,event):
if toggle4:
toggle4.remove()
else: toggle4.append(1)
def ToggleFive(self,event):
if toggle5:
toggle5.remove()
else: toggle5.append(1)
def ToggleSix(self,event):
if toggle6:
toggle6.remove()
else: toggle6.append(1)
app = wx.App(0)
ToggleButtons(None, -1, 'togglebuttons.py')
app.MainLoop()
答案 0 :(得分:0)
我会通过名称参数给每个ToggleButton一个唯一的名称,例如:
wx.ToggleButton(self, 6, '6', (20, 220), name="toggleOne")
然后在你的save方法中,循环显示按钮并将其名称和值保存到dict中:
mydict = {}
for btn in buttons:
mydict[btn.GetName()] = btn.GetValue()
然后在创建窗口时,检查已保存的文件,如果存在,请应用它。 ToggleButton有一个SetValue()方法,接受True或False,其中True表示切换。