好的,有点奇怪,我无法弄清楚。假设我有一些类别,每个类别都有一些项目,都都符合字典。
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()
(这是我的整个示例应用。) 我无法弄清楚原因,但一切都适用于字典中的最后一个类别: 但以前的类别只能显示一个按钮/项目
答案 0 :(得分:1)
thisPanel
正在循环中设置,但您在循环后调用SetSizer
,因此只有最后thisPanel
才能获得sizer。将呼叫移至循环内的SetSizer
。