0

有没有办法打开一个程序,点击几个切换按钮,保存已经切换的那些,然后如果你再次打开它,你可以从文件中读取并切换相同的按钮,这样你就可以继续或更改项目?

#!/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()
4

1 回答 1

0

我会通过它们的 name 参数给每个 ToggleButton 一个唯一的名称,例如:

wx.ToggleButton(self, 6, '6', (20, 220), name="toggleOne")

然后在您的保存方法中,遍历按钮并将它们的名称和值保存到字典中:

mydict = {}
for btn in buttons:
    mydict[btn.GetName()] = btn.GetValue()

然后,当您创建窗口时,检查保存的文件,如果存在,应用它。ToggleButton 有一个 SetValue() 方法,它接受 True 或 False,其中 True 表示已切换。

于 2013-08-12T18:08:35.787 回答