So first off, I have Cody Precord's book now. It looks useful. So
far all I've done with it was that I used it to dispose of a dead
lizard that my cats gave me just as I was opening the box from Amazon
that it came in, but I'm sure I'll find other uses for it, such as
trying to make my non-working scroll bars work.
But the matter at hand is sizers still. Your suggestions on how I was
failing to use sizers correctly were valuable, and I have made great
progress. I now have a window and a panel and some sizers and a
button-adding method that lay out the buttons correctly the first
time, then fails. The first time the window pops up it looks fine.
If I click on a button then window stays the same shape but all the
buttons pile up in the upper-left corner. If I resize the window then
they suddenly appear normal again. If I click a button then window
sizes back to its initial size. If I click another button, then they
all pile up in the upper-left corner again. I've tried Fit(),
Layout(), and Refresh() and none helped.
Sadly, I do not have a simple example for making this fail, only a
complex example, and the snippets below aren't nearly complete enough
to run or test.
The frame is set up with this:
buttonFrame = wx.Frame( None, title='Button Frame' )
self.buttonFrame = buttonFrame
buttonPanel = wx.Panel( buttonFrame, -1 )
buttonFrame.buttonPanel = buttonPanel
dirSizer = wx.GridSizer( cols = 3, hgap = 10, vgap = 10 )
buttonFrame.dirSizer = dirSizer
cmdSizer = wx.BoxSizer( wx.VERTICAL )
buttonFrame.cmdSizer = cmdSizer
panelSizer = wx.BoxSizer( wx.VERTICAL )
buttonFrame.panelSizer = panelSizer
panelSizer.Add( dirSizer, 1, wx.EXPAND )
panelSizer.AddSpacer( 20 )
panelSizer.Add( cmdSizer, 1, wx.EXPAND )
buttonPanel.SetSizer( panelSizer )
frameSizer = wx.BoxSizer()
buttonFrame.frameSizer = frameSizer
frameSizer.Add( buttonPanel, 1, wx.EXPAND )
buttonFrame.SetSizer( frameSizer )
buttonFrame.SetInitialSize()
buttonFrame.Show( True )
And the buttons are actually created and destroyed with this:
def updateButtons( self, interactions ):
def _findDir( interactions, dir ):
for i in interactions:
if ('character:move:%s' % dir) == i.hint():
return i
return None
buttonFrame = self.buttonFrame
buttonFrame.dirSizer.Clear( True )
buttonFrame.cmdSizer.Clear( True )
···
##
## put the cardinals up first
##
for c in ['', 'Up', '',
'NW', 'N', 'NE',
'W','Wait','E',
'SW','S','SE',
'', 'Down', '']:
b = wx.Button( buttonFrame.buttonPanel, label=c )
i = _findDir( interactions, c )
if not c or not i:
b.Enable( False )
else:
b.Bind( wx.EVT_BUTTON, self._buttonClick )
b.interact = i
buttonFrame.dirSizer.Add( b )
##
## Now place the other buttons
##
for i in interactions:
if i.hint() and i.hint().startswith( 'character:move:' ): continue
b = wx.Button( buttonFrame.buttonPanel, label=i.title() )
b.Bind( wx.EVT_BUTTON, self._buttonClick )
b.interact = i
buttonFrame.cmdSizer.Add( b )
buttonFrame.SetInitialSize()
buttonFrame.Fit()
buttonFrame.Layout()
buttonFrame.Refresh()
I'm stumped by the fact it lays it out fine if the window is the wrong
size but not if it is the right size. In fact, I can "fix" it just by
putting "buttonFrame.SetSize( (100,100) )" at the start of
updateButtons(). Not really an ideal solution though.
There is a zipfile at: http://infohost.nmt.edu/~schlake/games/x34/x34.zip
The two python files individually are:
http://infohost.nmt.edu/~schlake/games/x34/game.txt
http://infohost.nmt.edu/~schlake/games/x43/wxgui.txt
--
-- Schlake