In response to a comment by XilyummY, I have added this additional answer to show how the main classes can be organised in separate files.
This is my solution based on four files:
- main.py: the main frame for the application and the application loader;
- main_panel.py: the main panel for the application;
- menu_bar.py: the menu bar definition for the frame;
- tool_bar.py: the tool bar from the frame.
The code follows in this order:
main.py
import wx
from main_panel import MainPanel
from menu_bar import MenuBar
from tool_bar import MainToolbar
class MainFrame(wx.Frame):
"""Create MainFrame class."""
def __init__(self, *args, **kwargs):
super(MainFrame, self).__init__(None, *args, **kwargs)
self.Title = 'Basic wxPython module'
self.SetMenuBar(MenuBar(self))
self.ToolBar = MainToolbar(self)
self.status_bar = StatusBar(self).status_bar
self.Bind(wx.EVT_CLOSE, self.on_quit_click)
panel = MainPanel(self)
sizer = wx.BoxSizer()
sizer.Add(panel)
self.SetSizerAndFit(sizer)
self.Centre()
self.Show()
def on_quit_click(self, event):
"""Handle close event."""
del event
wx.CallAfter(self.Destroy)
class StatusBar(object):
def __init__(self, parent):
"""Create status bar."""
self.status_bar = parent.CreateStatusBar()
if __name__ == '__main__':
"""Run the application."""
screen_app = wx.App()
main_frame = MainFrame()
screen_app.MainLoop()
main_panel.py
import wx
class MainPanel(wx.Panel):
"""Panel class to contain frame widgets."""
def __init__(self, parent, *args, **kwargs):
super(MainPanel, self).__init__(parent, *args, **kwargs)
"""Create and populate main sizer."""
sizer = wx.BoxSizer(wx.VERTICAL)
cmd_quit = wx.Button(self, id=wx.ID_EXIT)
cmd_quit.Bind(wx.EVT_BUTTON, parent.on_quit_click)
sizer.Add(cmd_quit)
self.SetSizer(sizer)
menu_bar.py
import wx
class MenuBar(wx.MenuBar):
"""Create the menu bar."""
def __init__(self, parent, *args, **kwargs):
super(MenuBar, self).__init__(*args, **kwargs)
# File menu
file_menu = wx.Menu()
self.Append(file_menu, '&File')
quit_menu_item = wx.MenuItem(file_menu, wx.ID_EXIT)
parent.Bind(wx.EVT_MENU, parent.on_quit_click, id=wx.ID_EXIT)
file_menu.Append(quit_menu_item)
tool_bar.py
import wx
class MainToolbar(wx.ToolBar):
"""Create tool bar."""
def __init__(self, parent, *args, **kwargs):
super(MainToolbar, self).__init__(parent, *args, **kwargs)
new_bmp = wx.ArtProvider.GetBitmap(wx.ART_NEW)
#preferences_bmp = wx.Bitmap('images/preferences.png')
quit_bmp = wx.ArtProvider.GetBitmap(wx.ART_QUIT)
self.AddTool(wx.ID_NEW, 'New', new_bmp)
#self.AddTool(wx.ID_PREFERENCES, 'Preferences', preferences_bmp)
self.AddTool(wx.ID_EXIT, 'Quit', quit_bmp)
self.SetToolShortHelp(wx.ID_NEW, 'New ...')
self.SetToolShortHelp(wx.ID_PREFERENCES, 'Preferences ...')
self.SetToolShortHelp(wx.ID_EXIT, 'Quit')
self.Bind(wx.EVT_TOOL, parent.on_quit_click, id=wx.ID_EXIT)
self.Realize()