I’m writing a document based application in wxPython, by which I mean that the user can have open multiple documents at once in multiple windows or tabs. There are multiple kinds of documents, and the documents can all be in different “states”, meaning that there should be different menu options available in the main menu.
I know how to disable and enable menu items using the wx.EVT_UPDATE_UI event, but I can’t figure out how to pull off a main menu that changes structure and content drastically based on which document that currently has focus. One of my main issues is that the main menu is created in the top level window, and it has to invoke methods in grand children and great grand children that haven’t even been created yet.
Contrived example; when a document of type “JPEG” is open, the main menu should look like:
File Edit Compression Help
And when the user switches focus (CTRL+Tab) to a document of type “PDF”, the main menu should change to:
File Edit PDF Publish Help
And the “Edit” menu should contain some different options from when the “JPEG” document was in focus.
Current I’m just creating the menu in a function called create_main_menu in the top level window, and the document panels have no control over it. What would be necessary to pull off the kind of main menu scheme I describe above, specifically in wxPython?
I’ve figured out a pretty clean way to do this. First of all I create my “base” main menu bar, which contains the File and Help menu items. Then I defined a class
EditorPanelthan is a subclass ofwx.Paneland defined the methodsbind_main_menu_barandrelease_main_menu_bar. The first of those methods receives the main menu bar when the panel is focused, and adds some items to it. Here is one of my implementations:Now, when that editor panel is opened, the main menu receives an Edit menu and a Tools menu that is bound to event handlers in the
EditorPanel, which is incredibly handy. When the editor loses focus, therelease_main_menu_barmethod is called, which should restore the main menu bar to it’s original state. This is counterpart of the code above:So every editor that wants to edit the main menu just has to subclass those two methods and they have full control. The main frame will monitor when the user switches between editors and call the methods accordingly. The biggest problem was to figure out when the editor panel receives and loses focus, which is the topic of another question of mine: How do I monitor when wx.Panel receives and loses focus?