Menu, Status Bar, Tool Bar — wxGlade 0.9.5 documentation

# Menu, Status Bar, Tool Bar

## Menu Editor

wxGlade includes a simple menu editor.

To attach a menu to a frame, go to Properties -> Widget and check Has MenuBar.
This will add a menubar icon to the Tree, just below the frame’s icon.
To open the menu editor, click the “Edit menus…” button.

The following screenshots are from the file wxglade/examples/Allwidgets_28.wxg.

Properties Window: Example of a frame with a menu bar `Has MenuBar` is checked |
Tree Window: Example of a frame with a menu bar |
Properties Window: Press the “Edit menus…” button to open the menu editor |
Menu Editor The bottom part just lists the items, where the hierarchy is visualized by indentation. |
The Menu (In the example, “Unix” and Windows” are radio type menu items.) |

Example:

As an exercise, we will now add a “File” menu with two entries to our calculator window.

> - When you hit “Edit menus…” for the first time, the bottom part of the editor window is almost empty. It will just contain a default entry “item”.
> - To create the required menu structure, change the label to File.
> - To create the first item, hit “Add” and then the “>” button to turn it into a submenu item and then change the label to Reset. Give this item a name i_reset. The item will then be stored with this attribute name, such that it can e.g. enabled and disabled programmatically.
> - Create an item Exit with event handler on_menu_File_Exit

As of now, these items would not yet call any code when selected. So the “Event Handler” field needs to be filled with e.g. on_menu_File_Reset and on_menu_File_Exit for the two items.

When done and after hitting the “Start generating source files”, the editor and the created code should look like this:

Menu Editor with two items: For the Reset item, we set a name i_reset. |
Generated code including two event handlers The Reset menu item is assigned to `self.frame_menubar.i_reset` such that it can be accessed easily, e.g. for disabling it. |
    class CalculatorFrame(wx.Frame):
        def __init__(self, *args, **kwds):
            # begin wxGlade: CalculatorFrame.__init__
            kwds["style"] = wx.DEFAULT_FRAME_STYLE
            wx.Frame.__init__(self, *args, **kwds)
            ...
            # Menu Bar
            self.frame_menubar = wx.MenuBar()
            wxglade_tmp_menu = wx.Menu()
            self.frame_menubar.i_reset = \
                wxglade_tmp_menu.Append(wx.ID_ANY, "Reset", "Reset results")
            self.Bind( wx.EVT_MENU, self.on_menu_File_Reset,
                       id=self.frame_menubar.i_reset.GetId() )
            item = wxglade_tmp_menu.Append(wx.ID_ANY, "Exit", "Exit application")
            self.Bind( wx.EVT_MENU, self.on_menu_File_Exit, id=item.GetId() )
            self.frame_menubar.Append(wxglade_tmp_menu, "File")
            self.SetMenuBar(self.frame_menubar)
            # Menu Bar end
            ...

        def on_menu_File_Reset(self, event):  # wxGlade: MyFrame.<event_handler>
            print("Event handler 'on_menu_File_Reset' not implemented!")
            event.Skip()

        def on_menu_File_Exit(self, event):  # wxGlade: MyFrame.<event_handler>
            print("Event handler 'on_menu_File_Exit' not implemented!")
            event.Skip()
Handler implementation in derived class including initial disabling of `self.frame_menubar.i_reset` |
    class MyFrame(CalculatorFrame):
        def __init__(self, *args, **kwds):
            CalculatorFrame.__init__(self, *args, **kwds)
            # insert more initialization code here
            self.frame_menubar.i_reset.Enable(False)

        def on_menu_File_Reset(self, event):
            self.text_result.Clear()
            self.frame_menubar.i_reset.Enable(False)   # cleared already

        def on_menu_File_Exit(self, event):
            self.Close()

        def on_execute_button_clicked(self, event):
            # ....
            self.frame_menubar.i_reset.Enable(True)
            event.Skip()

        def on_reset_button_clicked(self, event):
            self.text_result.Clear()
            self.frame_menubar.i_reset.Enable(False)   # cleared already
            event.Skip()

You can implement the handler either in a derived class or directly in the file that wxGlade has written.
In the latter case, you should have enabled Properties -> Application -> Keep user sources.

The example menu is part of the example at wxglade/examples/Calculator:
  - Calculator-07-Import.wxg.
  - Calculator_GUI.py.
  - Calculator_Main.py.

## Status Bar Editor

To attach a status bar to a frame, go to Properties -> Widget and check Has StatusBar. This will add a statusbar icon to the Tree window, just below the frame’s icon. (Similar to Has MenuBar in the first screenshot on this page.)

To add/remove fields to the status bar, go to Properties -> Widget -> Fields and use the “Add”/”Insert”/”Remove”/”Apply” buttons. If you set the “Size” of a field to a negative value like -1 or -2, it will grow to fill the available space.

Example:

Statusbar: Properties / Field Editor: two growing and two fixed size fields |
Toolbar two growing and two fixed size fields |

## Tool Bar Editor

The logic for creating and editing a toolbar is the same as with menu bars.

Buttons and other controls are not supported yet