我有两个框架,第二个框架有一些复选框。当我关闭第二帧时如何保留我的选择?
这是一个示例代码!
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test",size=(300,200))
self.Btn = wx.Button(self, label="New",pos=(10,10),size=(30,30))
self.Bind(wx.EVT_BUTTON, self.new, self.Btn)
def new(self, event):
newFrame().Show()
class newFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test 2",size=(300,200))
self.ch0 = wx.CheckBox(self, -1,"Choice 1",pos=(10,10))
self.Bind(wx.EVT_CHECKBOX, self.OnCB0, self.ch0)
self.ch1 = wx.CheckBox(self, -1,"Choise 2",pos=(10,30))
self.Bind(wx.EVT_CHECKBOX, self.OnCB1, self.ch1)
self.ch2 = wx.CheckBox(self, -1,"Choise 3",pos=(10,50))
self.Bind(wx.EVT_CHECKBOX, self.OnCB2, self.ch2)
def OnCB0(self, event):
if self.ch0.IsChecked():
print "Checked 1"
else:
print "UnChecked 1"
def OnCB1(self, event):
if self.ch1.IsChecked():
print "Checked 2"
else:
print "UnChecked 2"
def OnCB2(self, event):
if self.ch2.IsChecked():
print "Checked 3"
else:
print "UnChecked 3"
if __name__ == '__main__':
app = wx.App(False)
app.frame = MainFrame()
app.frame.Show()
app.MainLoop()
我在这里发了类似的帖子:
wxPython: How the radio buttons can remember my choice when I close the frame
但它不起作用。
#### EDIT ####
我做了一些更改,但仍然无法正常工作。现在,当我再次打开第二帧时,它只保留一个选项,我检查的最后一个。我怎样才能让它拥有多个选择呢?
import wx
hold = None
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test",size=(300,200))
self.Btn = wx.Button(self, label="New",pos=(10,10),size=(30,30))
self.Bind(wx.EVT_BUTTON, self.new, self.Btn)
def new(self, event):
newFrame().Show()
class newFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test 2",size=(300,200))
self.ch0 = wx.CheckBox(self, -1,"Choice 1",pos=(10,10))
self.Bind(wx.EVT_CHECKBOX, self.OnCB0, id=self.ch0.GetId())
self.ch1 = wx.CheckBox(self, -1,"Choise 2",pos=(10,30))
self.Bind(wx.EVT_CHECKBOX, self.OnCB1, id=self.ch1.GetId())
self.ch2 = wx.CheckBox(self, -1,"Choise 3",pos=(10,50))
self.Bind(wx.EVT_CHECKBOX, self.OnCB2, id=self.ch2.GetId())
global hold
if hold is not None:
getattr(self,"ch"+str(int(hold[1])-1)).SetValue(True)
def OnCB0(self, event):
global hold
hold = 'd1/'
if self.ch0.IsChecked():
print "Checked 1"
else:
print "UnChecked 1"
def OnCB1(self, event):
global hold
hold = 'd2/'
if self.ch1.IsChecked():
print "Checked 2"
else:
print "UnChecked 2"
def OnCB2(self, event):
global hold
hold = 'd3/'
if self.ch2.IsChecked():
print "Checked 3"
else:
print "UnChecked 3"
if __name__ == '__main__':
app = wx.App(False)
app.frame = MainFrame()
app.frame.Show()
app.MainLoop()
答案 0 :(得分:1)
正如Joran在您指出的问题中所描述的那样,您需要不销毁新帧并每次重新创建它。然后它将能够记住检查的内容。请注意,默认情况下单击窗口管理器“关闭”按钮(“x”)会破坏框架。
import wx
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test",size=(300,200))
self.Btn = wx.Button(self, label="New",pos=(10,10),size=(30,30))
self.Bind(wx.EVT_BUTTON, self.show_newFrame, self.Btn)
# create the newFrame instance, keep it around.
self.new_frame = newFrame()
def show_newFrame(self, event):
self.new_frame.Show()
class newFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test 2",size=(300,200))
self.ch0 = wx.CheckBox(self, -1,"Choice 1",pos=(10,10))
self.Bind(wx.EVT_CHECKBOX, self.OnCB0, self.ch0)
self.ch1 = wx.CheckBox(self, -1,"Choise 2",pos=(10,30))
self.Bind(wx.EVT_CHECKBOX, self.OnCB1, self.ch1)
self.ch2 = wx.CheckBox(self, -1,"Choise 3",pos=(10,50))
self.Bind(wx.EVT_CHECKBOX, self.OnCB2, self.ch2)
self.Bind(wx.EVT_CLOSE, self.onClose)
# hide ourself on close, don't destroy
def onClose(self, event):
self.Hide()
def OnCB0(self, event):
if self.ch0.IsChecked():
print "Checked 1"
else:
print "UnChecked 1"
def OnCB1(self, event):
if self.ch1.IsChecked():
print "Checked 2"
else:
print "UnChecked 2"
def OnCB2(self, event):
if self.ch2.IsChecked():
print "Checked 3"
else:
print "UnChecked 3"
if __name__ == '__main__':
app = wx.App(False)
app.frame = MainFrame()
app.frame.Show()
app.MainLoop()
答案 1 :(得分:1)
您的第二个版本无法正常工作的原因是您只使用一个变量来尝试保存三个复选框的状态。
显然,变量名为" hold"只能包含一个要保留的复选框名称 - 代码的工作方式,它会保存最后一个按下的复选框的名称。
如果你坚持这样做,你将不得不做这样的事情:
import wx
hold = [None, None, None]
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test",size=(300,200))
self.Btn = wx.Button(self, label="New",pos=(10,10),size=(30,30))
self.Bind(wx.EVT_BUTTON, self.new, self.Btn)
def new(self, event):
newFrame().Show()
class newFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, wx.ID_ANY, "Test 2",size=(300,200))
self.ch0 = wx.CheckBox(self, -1,"Choice 1",pos=(10,10))
self.Bind(wx.EVT_CHECKBOX, self.OnCB0, id=self.ch0.GetId())
self.ch1 = wx.CheckBox(self, -1,"Choise 2",pos=(10,30))
self.Bind(wx.EVT_CHECKBOX, self.OnCB1, id=self.ch1.GetId())
self.ch2 = wx.CheckBox(self, -1,"Choise 3",pos=(10,50))
self.Bind(wx.EVT_CHECKBOX, self.OnCB2, id=self.ch2.GetId())
global hold
for hold_item in hold:
if hold_item:
getattr(self,"ch"+str(int(hold_item[1])-1)).SetValue(True)
def OnCB0(self, event):
global hold
if self.ch0.IsChecked():
print "Checked 1"
hold[0] = 'd1/'
else:
print "UnChecked 1"
hold[0] = None
def OnCB1(self, event):
global hold
if self.ch1.IsChecked():
print "Checked 2"
hold[1] = 'd2/'
else:
print "UnChecked 2"
hold[1] = None
def OnCB2(self, event):
global hold
if self.ch2.IsChecked():
print "Checked 3"
hold[2] = 'd3/'
else:
print "UnChecked 3"
hold[2] = None
if __name__ == '__main__':
app = wx.App(False)
app.frame = MainFrame()
app.frame.Show()
app.MainLoop()
然而,这是不可取的,因为: