Navigation

  • index
  • next |
  • previous |
  • wxGlade 1.1.1 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
AllWidgets_28_Properties_w_MenuBar
Tree Window:

Example of a frame with a menu bar
AllWidgets_28_Tree_w_MenuBar
Properties Window:

Press the “Edit menus…” button
to open the menu editor
AllWidgets_28_Properties_EditMenus
Menu Editor

The bottom part just lists the items,
where the hierarchy is visualized by indentation.
AllWidgets_28_MenuEditor
The Menu

(In the example, “Unix” and Windows”
are radio type menu items.)
AllWidgets_28_MenuPreview

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.
_images/Calculator07_Menu_Editor.png
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.

Lambda Event Handlers¶

When creating Python code, you may also specify a lambda function as event handler.

E.g. you may want to have three menu items named Insert A, Insert B and Insert C. Instead of three separate handlers, you may enter three lambda functions that will call the same method or function with different arguments each:

lambda evt: self.on_menu_insert_abc("A")

Of course you need to implement a method on_menu_insert_abc.

Status Bar Editor¶

To attach a status bar to a frame, go to Properties -> Widget and check cHas 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
Example Field List
Toolbar


two growing and two fixed size fields
Example Status Bar

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

Logo

Table Of Contents

  • Menu, Status Bar, Tool Bar
    • Menu Editor
      • Lambda Event Handlers
    • Status Bar Editor
    • Tool Bar Editor

Previous topic

Custom Widget

Next topic

Bitmaps

This Page

  • Show Source

Quick search

Navigation

  • index
  • next |
  • previous |
  • wxGlade 1.1.1 documentation »
© Copyright 2017-2025, Dietmar Schwertberger. Created using Sphinx 1.6.4.