好吧,有些奇怪,我无法弄清楚。假设我有一些类别,每个类别都有一些项目,都适合字典。
import wx
nyoom = {}
nyoom['spiders'] = ['Yellow Sac', 'Orbweaver', 'Garden']
nyoom['cats']= ['Bombay','Angora']
nyoom['cambrian'] = ['Wiwaxia','Hallucigenia','Pikaia','Alomancaris']
然后我想将每个类别显示为一个按钮,该按钮将该类别的所有内容也折叠或显示为按钮。
class SeqFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__( self, None, wx.ID_ANY, "GUI Woes", size=(400,400))
self.panel = wx.Panel(self, wx.ID_ANY)
sizer = wx.FlexGridSizer(cols=2, hgap=5, vgap=5) #sizer for the window
numActsTxtBox = wx.TextCtrl( self.panel, -1, "1", size=(40, -1))
numActsTxtTxt = wx.CheckBox( self.panel, -1, "Number of Times")
sizer.Add( numActsTxtTxt, 0, wx.ALL )
sizer.Add( numActsTxtBox, 0, wx.ALL )
for categories in nyoom.keys():
self.categPanes=[]
self.categPanes.append(wx.CollapsiblePane(self.panel, -1, label=categories))
self.buildPanes(self.categPanes[-1], sizer)
thisPanel = self.categPanes[-1].GetPane()
panelSizer = wx.BoxSizer( wx.VERTICAL ) #sizer for the panel contents
sortedCategory = nyoom[categories]
sortedCategory.sort()
for i in range(0,len(nyoom[categories])):
thisSeq = wx.Button( thisPanel, label=sortedCategory[i], name=sortedCategory[i])
panelSizer.Add( thisSeq, 0, wx.GROW | wx.ALL ,0 )
thisPanel.SetSizer(panelSizer)
panelSizer.SetSizeHints(thisPanel)
self.panel.SetSizerAndFit(sizer)
self.Fit()
def buildPanes(self, categPane, sizer):
categPane.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, self.OnPaneChanged)
sizer.Add(categPane, 0, wx.GROW | wx.ALL, 0)
def OnPaneChanged(self, evt):
self.panel.GetSizer().Layout()
self.panel.Fit()
self.Fit()
if __name__ == '__main__':
app = wx.App()
frame = SeqFrame()
frame.Show(True)
app.MainLoop()
(这是我的整个示例应用程序。)我不知道为什么,但字典中的最后一个类别一切正常:
但前面的类别只能显示一个按钮/项目。