agw.aui.AUIToolbar: how do i resize it correct?

I have a agw.aui.AUIToolbar with a wx.Choice control added. When creating (.Realize) the toolbar, the choice control has no items. Later when i add items, the control is not wide enough to display the item label. See the example code for my solution to resize the toolbar (on_update_toolbar).

Is there a shorter way of doing it? And why do i need to call self.aui_mgr.Update() twice?


toolbar_choice_sample.py (1.8 KB)

import wx
import wx.lib.agw.aui as aui
import atexit
from wx.lib.mixins.inspection import InspectableApp


note = "press [toolbar_choice_sample.py|attachment](upload://AeoibYw4KaFyYggc2GPsxkAYTui.py) (1.8 KB) the + button in the toolbar\nto resize/rearrange the toolbar"


class TestFrame(wx.Frame):
    def __init__(self):
        super(TestFrame, self).__init__(None, size=(600, 300))

        self.aui_mgr = aui.AuiManager_DCP()
        self.aui_mgr.SetManagedWindow(self)
        atexit.register(self.aui_mgr.UnInit)

        main_book = aui.AuiNotebook(self)
        main_book.AddPage(wx.StaticText(main_book, label=note), "note")
        self.tb = aui.AuiToolBar(self)
        self.tb.SetName('Toolbar_Device')
        self.chc = wx.Choice(self.tb)
        self.tb.AddControl(self.chc)
        self.tb.Realize()

        tb2 = aui.AuiToolBar(self)
        tb2.AddSimpleTool(
            tool_id=wx.NewIdRef(),
            label='add_items',
            bitmap=wx.ArtProvider().GetBitmap(wx.ART_PLUS),
            short_help_string="resize/rearrange toolbar",
        )
        tb2.Realize()

        self.aui_mgr.AddPane(main_book, aui.AuiPaneInfo().Center())
        self.aui_mgr.AddPane(self.tb, aui.AuiPaneInfo().Top().ToolbarPane())
        self.aui_mgr.AddPane(tb2, aui.AuiPaneInfo().Top(). ToolbarPane())

        self.Bind(wx.EVT_TOOL, self.on_update_toolbar)

    def on_update_toolbar(self, evt):
        # the following needs to be done to resize/rearrange the toolbar
        tb_item = self.tb.FindToolByIndex(0)
        tb_item.SetMinSize(self.chc.GetBestSize())
        self.tb.Realize()
        self.aui_mgr.DoFrameLayout()
        self.aui_mgr.Update()
        self.aui_mgr.Update()  # a second Update() is needed


app = InspectableApp()
win = TestFrame()
win.Show()
win.chc.SetItems(["item with a very long label", 'another item', 'and a last item'])
win.chc.SetSelection(0)
app.MainLoop()