Created
October 19, 2018 17:34
-
-
Save raceybe/c3c493c908124dacad0ccbdaf52d336d to your computer and use it in GitHub Desktop.
wxPython Menubar, Toolbar, Statusbar, Panels, Nested Sizers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wx | |
class MainFrame(wx.Frame): | |
def __init__(self, *args, **kwargs): | |
super(MainFrame, self).__init__(*args, **kwargs) | |
# Setup the frame | |
self.SetSize(1366, 768) | |
self.SetTitle('Panel Layout') | |
self.Center() | |
# Create the menu bar and menus | |
menubar = wx.MenuBar() | |
file_menu = wx.Menu() | |
file_item = file_menu.Append(wx.ID_EXIT, 'Quit', 'Quit application') | |
menubar.Append(file_menu, '&File') | |
self.SetMenuBar(menubar) | |
self.Bind(wx.EVT_MENU, self.on_quit, file_item) | |
# Create the tool bar and icons | |
toolbar_top = wx.ToolBar(self, style=(wx.TB_FLAT)) | |
quit_tool = toolbar_top.AddTool(wx.ID_ANY, 'Exit', wx.ArtProvider.GetBitmap( | |
wx.ART_QUIT, wx.ART_TOOLBAR, (24, 24)), wx.NullBitmap, wx.ITEM_NORMAL, 'Exit', 'Long help for \'Open\'', None) | |
toolbar_top.Realize() | |
self.Bind(wx.EVT_TOOL, self.on_quit, quit_tool) | |
# Create the status bar | |
statusbar = self.CreateStatusBar() | |
# Top panel | |
panel_top = wx.Panel(self) | |
panel_top.BackgroundColour = 'purple' | |
label_textbox_top = wx.StaticText(panel_top, label='Top Textbox') | |
label_textbox_top.BackgroundColour = 'white' | |
sizer_top = wx.BoxSizer() | |
sizer_top.Add(label_textbox_top) | |
panel_top.SetSizer(sizer_top) | |
# Bottom panel | |
panel_bottom = wx.Panel(self) | |
panel_bottom.BackgroundColour = 'red' | |
label_textbox_bottom = wx.StaticText( | |
panel_bottom, label='Bottom Textbox') | |
label_textbox_bottom.BackgroundColour = 'white' | |
sizer_bottom = wx.BoxSizer() | |
sizer_bottom.Add(label_textbox_bottom) | |
panel_bottom.SetSizer(sizer_bottom) | |
# Right panel | |
panel_right = wx.Panel(self) | |
panel_right.BackgroundColour = 'green' | |
label_textbox_right = wx.StaticText(panel_right, label='Right Textbox') | |
label_textbox_right.BackgroundColour = 'white' | |
sizer_right = wx.BoxSizer() | |
sizer_right.Add(label_textbox_right) | |
panel_right.SetSizer(sizer_right) | |
# Create sizers | |
sizer_main = wx.BoxSizer(wx.VERTICAL) | |
sizer_left_right = wx.BoxSizer(wx.HORIZONTAL) | |
sizer_top_bottom = wx.BoxSizer(wx.VERTICAL) | |
# Nest appropriate panels and sizers | |
sizer_top_bottom.Add(panel_top, 1, wx.EXPAND) | |
sizer_top_bottom.Add(panel_bottom, 1, wx.EXPAND) | |
sizer_left_right.Add(sizer_top_bottom, 1, wx.EXPAND) | |
sizer_left_right.Add(panel_right, 1, wx.EXPAND) | |
sizer_main.Add(toolbar_top, 0, wx.EXPAND) | |
sizer_main.Add(sizer_left_right, 1, wx.EXPAND) | |
# Set main sizer | |
self.SetSizer(sizer_main) | |
def on_quit(self, e): | |
self.Close() | |
def main(): | |
import wx.lib.inspection | |
app = wx.App() | |
mainframe = MainFrame(None) | |
mainframe.Show() | |
wx.lib.inspection.InspectionTool().Show() | |
app.MainLoop() | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment