Dear wxPythonians,
To experiment with the new Listbook control before incorporating it into a real project, I created a small program that starts with a one-panel Listbook and then adds new panels in response to a button click. It works, but with three serious size-and-layout-related problems:
1) When the application launches, it is sized incorrectly. Apparently the Listbook isn't aware of the size of its contents, and opens with a tiny frame that displays little more than the item title in the list on the left (that it, is does not display most of the panel associated with that item). If I resize the window manually, all the controls are there and laid out correctly, but I shouldn't have to resize it manually. When I look at the relevant part of the wxPython demo code, I see a snippet that seems to refer to size events, but a) my problem involves not resizing, but initial sizing, and b) I'm not a sufficiently experienced Python programmer to understand how the demo code works, or how to transfer the relevant logic into the context of my sample program. If the problem is just a matter of user ignorance that can be resolved by inserting a particular line or two of code in a particular place, I'd be grateful if someone could point that out.
2) When I add new pages to the Listbook by clicking the button, the names of the items in the list itself have a lot of vertical space between them. Because the real project will have a lot of names in the list and I'd like to minimize scrolling, I need the list to be packed tightly, with no extra vertical space between the items, but I don't know how to override the apparent default spacing. As far as I can see, the spacing is also present on the demo, but it makes sense there, since the item names have associated images, and the extra space below each item makes it clear that the names go with the images above them, rather than below them. (Furthermore, the demo seems to construct an image list and assign it to the Listbook, which I don't do.) In any case, in a list that consists merely of text, the extra vertical spacing is awkward.
3) The names of the items in the list seem to come out centered in my code, as they do in the demo. That makes sense in the demo, since it centers the name under the associated image, but I need the names to be left-justified, as one might expect from a plain text list without images.
The code is fairly short, so I've included it below, and I'd be grateful for suggestions. In case it matters, I'm running on Microsoft Windows 2000, using ActivePython 2.3.2 and wxPython 2.5.1.
Thanks,
David
djbpitt+python@pitt.edu
···
________
The code is in two modules. The first (main) one is below; on my system it's called "ListbookMinimal.py".
---------------------------------------------------------------------------
----------
#!/usr/bin/env python
import wx
from ListbookPanel import ListbookPanel
class MyFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.panel_1 = wx.Panel(self, -1)
self.listbook_1 = wx.Listbook(self.panel_1, -1, style=0)
self.listbook_1_pane_1 = wx.Panel(self.listbook_1, -1)
self.label_3 = wx.StaticText(self.listbook_1_pane_1, -1, "label_3")
self.text_ctrl_3 = wx.TextCtrl(self.listbook_1_pane_1, -1, "")
self.label_4 = wx.StaticText(self.listbook_1_pane_1, -1, "label_4")
self.text_ctrl_4 = wx.TextCtrl(self.listbook_1_pane_1, -1, "")
self.label_5 = wx.StaticText(self.listbook_1_pane_1, -1, "label_5")
self.text_ctrl_5 = wx.TextCtrl(self.listbook_1_pane_1, -1, "")
self.button_1 = wx.Button(self.panel_1, -1, "Add Page")
self.__set_properties()
self.__do_layout()
self.Bind(wx.EVT_BUTTON, self.onClick, self.button_1)
def __set_properties(self):
self.SetTitle("Listbook Test")
def __do_layout(self):
sizer_3 = wx.BoxSizer(wx.HORIZONTAL)
sizer_4 = wx.BoxSizer(wx.VERTICAL)
sizer_5 = wx.BoxSizer(wx.HORIZONTAL)
grid_sizer_1 = wx.FlexGridSizer(3, 2, 0, 0)
grid_sizer_1.Add(self.label_3, 0, 0, 0)
grid_sizer_1.Add(self.text_ctrl_3, 0, 0, 0)
grid_sizer_1.Add(self.label_4, 0, 0, 0)
grid_sizer_1.Add(self.text_ctrl_4, 0, 0, 0)
grid_sizer_1.Add(self.label_5, 0, 0, 0)
grid_sizer_1.Add(self.text_ctrl_5, 0, 0, 0)
sizer_5.Add(grid_sizer_1, 1, wx.EXPAND, 0)
self.listbook_1_pane_1.SetAutoLayout(1)
self.listbook_1_pane_1.SetSizer(sizer_5)
sizer_5.Fit(self.listbook_1_pane_1)
sizer_5.SetSizeHints(self.listbook_1_pane_1)
self.listbook_1.AddPage(self.listbook_1_pane_1, "tab1")
sizer_4.Add(self.listbook_1, 1, wx.EXPAND, 0)
sizer_4.Add(self.button_1, 0, wx.ALIGN_CENTER_HORIZONTAL, 0)
self.panel_1.SetAutoLayout(1)
self.panel_1.SetSizer(sizer_4)
sizer_4.Fit(self.panel_1)
sizer_4.SetSizeHints(self.panel_1)
sizer_3.Add(self.panel_1, 1, wx.EXPAND, 0)
self.SetAutoLayout(1)
self.SetSizer(sizer_3)
sizer_3.Fit(self)
sizer_3.SetSizeHints(self)
self.Layout()
def onClick(self,evt):
self.listbook_1.AddPage(ListbookPanel(self.listbook_1, -1), "hi", -1)
# end of class MyFrame
class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
self.SetTopWindow(frame_1)
frame_1.Show(1)
return 1
# end of class MyApp
if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()
---------------------------------------------------------------------------
----------
The second module is below, and is called ListbookPanel.py. Clicking the button adds new panels of this type.
---------------------------------------------------------------------------
----------
#!/usr/bin/env python
import wx
class ListbookPanel(wx.Panel):
def __init__(self, parent, *args, **kwds):
wx.Panel.__init__(self, parent, *args, **kwds)
mySizer = wx.BoxSizer(wx.HORIZONTAL)
self.myTextCtrl = wx.TextCtrl(self, -1, "Dummy text")
self.newTextCtrl = wx.TextCtrl(self, -1, "More text")
mySizer.Add(self.myTextCtrl, 0, 0, 0)
mySizer.Add(self.newTextCtrl, 0, 0, 0)
self.SetAutoLayout(True)
self.SetSizer(mySizer)
mySizer.Fit(self)
mySizer.SetSizeHints(self)