Reputation: 55
I am trying to create a floating button bar in wxPython using a wx.frame. I have started with 2 buttons as a prototype, but I can't get the frame itself to resize. Is panel.SetSizerAndFit(sizer) the correct statement to use?
import wx
class MainToolbar(wx.Frame):
def __init__(self):
super(MainToolbar, self).__init__(None, title='some title')
panel = wx.Panel(self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
buttonSizer = wx.GridSizer(rows=1, cols=2, vgap=1, hgap=1)
btn1 = wx.Button(panel, label='Ok', size=(100,100))
btn2 = wx.Button(panel, label='Close', size=(100,100))
buttonsArray = [ (btn1, 0, wx.EXPAND), (btn2, 0, wx.EXPAND) ]
buttonSizer.AddMany(buttonsArray)
sizer.Add(buttonSizer, proportion=1, flag=wx.EXPAND)
panel.SetSizerAndFit(sizer)
if __name__ == "__main__":
app = wx.App()
frame = MainToolbar()
frame.Show()
app.MainLoop()
Upvotes: 0
Views: 454
Reputation: 22458
Putting widgets in the sizers with proportion=1
and EXPAND
flags, is going to fill the available space.
Also, I'm not sure what wxPython does when you specify a size
and put the widget in a sizer
, I assume it runs with the implicit size instruction.
Clearly, I can't guess how you wish the widgets to react, if you resize the window but here's a bare bones way of getting it all to fit.
import wx
class MainToolbar(wx.Frame):
def __init__(self):
super(MainToolbar, self).__init__(None, title='some title')
panel = wx.Panel(self)
buttonSizer = wx.GridSizer(rows=1, cols=2, vgap=1, hgap=1)
btn1 = wx.Button(panel, label='Ok', size=(100,100))
btn2 = wx.Button(panel, label='Close', size=(100,100))
buttonsArray = [ (btn1), (btn2) ]
buttonSizer.AddMany(buttonsArray)
panel.SetSizer(buttonSizer)
mainsizer = wx.BoxSizer(wx.VERTICAL)
mainsizer.Add(panel)
self.SetSizerAndFit(mainsizer)
self.Show()
if __name__ == "__main__":
app = wx.App()
frame = MainToolbar()
app.MainLoop()
Upvotes: 1