I'm trying to look into wxPython and sizers, and I put together the following example:
import wx
class MyTestFrame(wx.Frame):
def __init__(self, parent, title):
super(MyTestFrame, self).__init__(parent, title=title,
size=(250, 150))
# the master panel of the frame - "Add a panel so it looks correct on all platforms"
self.panel = wx.Panel(self, wx.ID_ANY)
# self.panel.SetBackgroundColour(wx.Colour(124, 224, 124)) # to confirm the square is the panel
# want these buttons absolutely positioned
btn_A = wx.Button(self, id=1, label='A', pos=(10, 10), size=(30, 30))
btn_A.SetBackgroundColour(wx.Colour(224, 124, 124))
btn_B = wx.Button(self, id=2, label='B', pos=(45, 10), size=(30, 30))
btn_C = wx.Button(self, id=3, label='C', pos=(80, 10), size=(30, 30))
# additional object
mastersizer = wx.BoxSizer(wx.VERTICAL)
btnsizer = wx.BoxSizer(wx.HORIZONTAL)
btnsizer.Add(btn_A, 0)
btnsizer.Add(btn_B, 0)
btnsizer.Add(btn_C, 0)
mastersizer.Add(btnsizer, 1, wx.EXPAND)
self.panel.SetSizer(mastersizer)
#~ mastersizer.Fit(self) # makes the window as large as the buttons
self.Centre()
self.Show()
if __name__ == '__main__':
app = wx.App()
MyTestFrame(None, 'Test')
app.MainLoop()
When I run this, I get a window like on the image:
Can someone explain, why do I get that gray little square in the upper left corner - and what would be the correct way to implement the code? (The button is deliberately colored, so it can be obvious.. ) I'm on Ubuntu Lucid, in case this is platform specific.
EDIT: That square is apparently the panel itself, but then I cannot tell why doesn't it resize and become the 'parent' for the buttons, as intended?
Ah well - wasn't that bad; but wasn't that obvious to me :) So here's some reference for other noobs like myself: In the above code, the buttons are defined as children of the frame - not of the panel; so the only change is this:
# want these buttons absolutely positioned
# must be children of panel - if panel is to encompass them!
btn_A = wx.Button(self.panel, id=1, label='A', pos=(10, 10), size=(30, 30))
btn_A.SetBackgroundColour(wx.Colour(224, 124, 124))
btn_B = wx.Button(self.panel, id=2, label='B', pos=(45, 10), size=(30, 30))
btn_C = wx.Button(self.panel, id=3, label='C', pos=(80, 10), size=(30, 30))
And then all seems fine:
Well, sorry to have wasted space here - but hopefully, may be of use to others :)
Cheers!