4
votes

I am trying to create a wxPython frame that is just the size of the toolbar I created.

I have successfully created the wx.Frame and empty toolbar but the window is far too big, how do I make it just fit the toolbar.

Code I tried:

import wx

class Example(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)

        vbox = wx.BoxSizer(wx.VERTICAL)

        toolbar1 = wx.ToolBar(self)
        toolbar1.SetToolBitmapSize(wx.Size(64, 64))
        toolbar1.Realize()
        vbox.Add(toolbar1, 0, wx.EXPAND)
        self.SetSizer(vbox)
        self.SetTitle('Toolbars')
        self.Centre()
        vbox.Fit(self)

app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()
2

2 Answers

0
votes

Is it not possible to set the size of frame to size of tool bar. Something like.

self.size = toolbar1.size
0
votes

I'm would assume that the reason that the toolbar size on linux/mac is (0, 0) is b/c there are no tools attached to it. To workaround that you can set a minimum size to the toolbar and then calculate the frame size by adding the frame caption and borders to the toolbar size.

import wx


class Example(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)

        vbox = wx.BoxSizer(wx.VERTICAL)

        toolbar1 = wx.ToolBar(self)
        toolbar1.SetBackgroundColour(wx.RED)
        toolbar1.SetToolBitmapSize(wx.Size(64, 64))
        # prevent the sizer from setting the size to (0, 0) when there are no tools attached
        toolbar1.SetMinSize((64, 64))
        toolbar1.Realize()
        vbox.Add(toolbar1, 0, wx.EXPAND)
        self.SetSizer(vbox)
        self.SetTitle('Toolbars')
        self.Centre()
        vbox.Fit(self)
        self.Layout()

        # calculate the frame caption height
        caption_height = wx.SystemSettings.GetMetric(wx.SYS_CAPTION_Y, self)
        # *2 for the left and right border
        border_width = wx.SystemSettings.GetMetric(wx.SYS_BORDER_X, self) * 2

        # get the size of the toolbar
        sx, sy = toolbar1.GetSize()
        # set the frame size by adding the toolbar size to the border/caption size
        self.SetSize((sx + border_width, sy + caption_height))


app = wx.App()
ex = Example(None)
ex.Show()
app.MainLoop()