How to create a flat menu - AGW package (Phoenix)

Keywords : Flat menu, AGW, wx.lib.agw.flatmenu, Theme, Style, Custom.


Demonstrating :

Tested py3.x, wx4.x and Win10.

Are you ready to use some samples ? ;)

Test, modify, correct, complete, improve and share your discoveries ! (!)


Sample one

img_sample_one.png

Note : for Windows and Linux only.

   1 # sample_one.py
   2 
   3 import sys
   4 import os
   5 import wx
   6 import wx.lib.agw.flatmenu as FM
   7 
   8 # class MyMenuBar
   9 # class MyFrame
  10 # class MyApp
  11 
  12 #-------------------------------------------------------------------------------
  13 
  14 class MyMenuBar(FM.FlatMenuBar):
  15     """
  16     A custom renderer class for FlatMenu.
  17     """
  18     def __init__(self, parent):
  19         FM.FlatMenuBar.__init__(self, parent,
  20                                 id=wx.ID_ANY,
  21                                 iconSize=32,
  22                                 spacer=3,
  23                                 options=FM.FM_OPT_IS_LCD)
  24 
  25         #------------
  26 
  27         self.parent = parent
  28 
  29         #------------
  30 
  31         # File Menu.
  32         self.file_menu = FM.FlatMenu()
  33 
  34         # Create the menu items.
  35         item = FM.FlatMenuItem(self.file_menu,
  36                                wx.CLOSE,
  37                                "&Open\tCtrl+O",
  38                                "Open file",
  39                                wx.ITEM_NORMAL,
  40                                None)
  41         item.SetTextColour("black")
  42         self.file_menu.AppendItem(item)
  43         self.file_menu.AppendSeparator()
  44 
  45         # Create the menu items.
  46         item = FM.FlatMenuItem(self.file_menu,
  47                                wx.ID_EXIT,
  48                                "&Quit\tCtrl+Q",
  49                                "Quit the program",
  50                                wx.ITEM_NORMAL,
  51                                None)
  52         # Demonstrate how to set custom font
  53         # and text colour to a FlatMenuItem.
  54         item.SetFont(wx.Font(-1, wx.FONTFAMILY_DEFAULT,
  55                              wx.FONTSTYLE_NORMAL,
  56                              wx.FONTWEIGHT_BOLD,
  57                              False, ""))
  58         item.SetTextColour("#e18619")
  59 
  60         self.file_menu.AppendItem(item)
  61 
  62         #------------
  63 
  64         # Help Menu.
  65         self.help_menu = FM.FlatMenu()
  66 
  67         # Create the menu items.
  68         item = FM.FlatMenuItem(self.help_menu,
  69                                wx.ID_ABOUT,
  70                                "&About\tCtrl+A",
  71                                "About",
  72                                wx.ITEM_NORMAL,
  73                                None)
  74         item.SetTextColour("black")
  75         self.help_menu.AppendItem(item)
  76 
  77         #------------
  78 
  79         # Add menu to the menu bar.
  80         self.Append(self.file_menu, "&File")
  81         self.Append(self.help_menu, "&Help")
  82 
  83 #-------------------------------------------------------------------------------
  84 
  85 class MyFrame(wx.Frame):
  86     def __init__(self, parent, id):
  87         wx.Frame.__init__(self, parent, -1,
  88                           title="Main frame (no theme)")
  89 
  90         #------------
  91 
  92         # Return icons folder.
  93         self.icons_dir = wx.GetApp().GetIconsDir()
  94 
  95         #------------
  96 
  97         # Simplified init method.
  98         self.SetProperties()
  99         self.MakeMenuBar()
 100         self.CreateCtrls()
 101         self.BindEvents()
 102         self.DoLayout()
 103 
 104         #------------
 105 
 106         self.CenterOnScreen()
 107 
 108     #---------------------------------------------------------------------------
 109 
 110     def SetProperties(self):
 111         """
 112         ...
 113         """
 114 
 115         frameIcon = wx.Icon(os.path.join(self.icons_dir,
 116                                          "icon_wxWidgets.ico"),
 117                             type=wx.BITMAP_TYPE_ICO)
 118         self.SetIcon(frameIcon)
 119 
 120     def MakeMenuBar(self):
 121         """
 122         ...
 123         """
 124 
 125         self.menuBar = MyMenuBar(self)
 126 
 127 
 128     def CreateCtrls(self):
 129         """
 130         ...
 131         """
 132 
 133         self.mainPnl = wx.Panel(self, -1)
 134 
 135         self.btn = wx.Button(self.mainPnl, -1, "Close Me")
 136         self.btn.SetPosition((15, 15))
 137 
 138 
 139     def BindEvents(self):
 140         """
 141         ...
 142         """
 143 
 144         # Bind the close event to an event handler.
 145         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 146         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btn)
 147         self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=wx.ID_EXIT)
 148 
 149 
 150     def DoLayout(self):
 151         """
 152         ...
 153         """
 154 
 155         # MainSizer is the top-level one that manages everything.
 156         mainSizer = wx.BoxSizer(wx.VERTICAL)
 157 
 158         mbSizer = wx.BoxSizer(wx.HORIZONTAL)
 159         mbSizer.Add(self.menuBar, 1, wx.ALL, 0)
 160 
 161         mainSizer.Add(mbSizer, 0, wx.EXPAND, 0)
 162         mainSizer.Add(self.mainPnl, 1, wx.EXPAND, 0)
 163 
 164         # Finally, tell the panel to use the sizer for layout.
 165         self.SetSizer(mainSizer)
 166         self.Layout()
 167 
 168 
 169     def OnCloseMe(self, event):
 170         """
 171         ...
 172         """
 173 
 174         self.Close(True)
 175 
 176 
 177     def OnCloseWindow(self, event):
 178         """
 179         ...
 180         """
 181 
 182         self.Destroy()
 183 
 184 #-------------------------------------------------------------------------------
 185 
 186 class MyApp(wx.App):
 187     """
 188     ....
 189     """
 190     def OnInit(self):
 191 
 192         #------------
 193 
 194         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 195 
 196         #------------
 197 
 198         frame = MyFrame(None, -1)
 199         self.SetTopWindow(frame)
 200         frame.Show(True)
 201 
 202         return True
 203 
 204     #---------------------------------------------------------------------------
 205 
 206     def GetInstallDir(self):
 207         """
 208         Returns the installation directory for my application.
 209         """
 210 
 211         return self.installDir
 212 
 213 
 214     def GetIconsDir(self):
 215         """
 216         Returns the icons directory for my application.
 217         """
 218 
 219         icons_dir = os.path.join(self.installDir, "icons")
 220         return icons_dir
 221 
 222 #-------------------------------------------------------------------------------
 223 
 224 def main():
 225     app = MyApp(False)
 226     app.MainLoop()
 227 
 228 #-------------------------------------------------------------------------------
 229 
 230 if __name__ == "__main__" :
 231     main()


Sample two

img_sample_two.png

Note : for Windows and Linux only.

   1 # sample_two.py
   2 
   3 import sys
   4 import os
   5 import wx
   6 import wx.lib.agw.flatmenu as FM
   7 
   8 # class MyMenuBar
   9 # class MyFrame
  10 # class MyApp
  11 
  12 #-------------------------------------------------------------------------------
  13 
  14 class MyMenuBar(FM.FlatMenuBar):
  15     """
  16     A custom renderer class for FlatMenu.
  17     """
  18     def __init__(self, parent):
  19         FM.FlatMenuBar.__init__(self, parent,
  20                                 id=wx.ID_ANY,
  21                                 iconSize=32,
  22                                 spacer=3,
  23                                 options=FM.FM_OPT_IS_LCD)
  24 
  25         #------------
  26 
  27         self.parent = parent
  28 
  29         #------------
  30 
  31         # Style / Theme :
  32         # FM.StyleDefault - FM.Style2007,
  33         # FM.StyleXP - FM.StyleVista
  34         # self.newMyTheme = self.GetRendererManager().AddRenderer(MyMenuRenderer())
  35         self.GetRendererManager().SetTheme(FM.StyleVista)
  36 
  37         #------------
  38 
  39         # File Menu.
  40         self.file_menu = FM.FlatMenu()
  41 
  42         # Create the menu items.
  43         item = FM.FlatMenuItem(self.file_menu,
  44                                wx.CLOSE,
  45                                "&Save\tCtrl+S",
  46                                "Save file",
  47                                wx.ITEM_NORMAL,
  48                                None)
  49         item.SetTextColour("black")
  50         self.file_menu.AppendItem(item)
  51         self.file_menu.AppendSeparator()
  52 
  53         # Create the menu items.
  54         item = FM.FlatMenuItem(self.file_menu,
  55                                wx.ID_EXIT,
  56                                "&Quit\tCtrl+Q",
  57                                "Quit the program",
  58                                wx.ITEM_NORMAL,
  59                                None)
  60         # Demonstrate how to set custom font
  61         # and text colour to a FlatMenuItem.
  62         item.SetFont(wx.Font(-1, wx.FONTFAMILY_DEFAULT,
  63                              wx.FONTSTYLE_NORMAL,
  64                              wx.FONTWEIGHT_BOLD,
  65                              False, ""))
  66         item.SetTextColour("#d34725")
  67 
  68         self.file_menu.AppendItem(item)
  69 
  70         #------------
  71 
  72         # Help Menu.
  73         self.help_menu = FM.FlatMenu()
  74 
  75         # Create the menu items.
  76         item = FM.FlatMenuItem(self.help_menu,
  77                                wx.ID_ABOUT,
  78                                "&About\tCtrl+A",
  79                                "About",
  80                                wx.ITEM_NORMAL,
  81                                None)
  82         item.SetTextColour("black")
  83         self.help_menu.AppendItem(item)
  84 
  85         #------------
  86 
  87         # Add menu to the menu bar.
  88         self.Append(self.file_menu, "&File")
  89         self.Append(self.help_menu, "&Help")
  90 
  91 #-------------------------------------------------------------------------------
  92 
  93 class MyFrame(wx.Frame):
  94     def __init__(self, parent, id):
  95         wx.Frame.__init__(self, parent, -1,
  96                           title="Main frame with theme")
  97 
  98         #------------
  99 
 100         # Return icons folder.
 101         self.icons_dir = wx.GetApp().GetIconsDir()
 102 
 103         #------------
 104 
 105         # Simplified init method.
 106         self.SetProperties()
 107         self.MakeMenuBar()
 108         self.CreateCtrls()
 109         self.BindEvents()
 110         self.DoLayout()
 111 
 112         #------------
 113 
 114         self.CenterOnScreen()
 115 
 116     #---------------------------------------------------------------------------
 117 
 118     def SetProperties(self):
 119         """
 120         ...
 121         """
 122 
 123         frameIcon = wx.Icon(os.path.join(self.icons_dir,
 124                                          "icon_wxWidgets.ico"),
 125                             type=wx.BITMAP_TYPE_ICO)
 126         self.SetIcon(frameIcon)
 127 
 128 
 129     def MakeMenuBar(self):
 130         """
 131         ...
 132         """
 133 
 134         self.menuBar = MyMenuBar(self)
 135 
 136 
 137     def CreateCtrls(self):
 138         """
 139         ...
 140         """
 141 
 142         self.mainPnl = wx.Panel(self, -1)
 143 
 144         self.btn = wx.Button(self.mainPnl, -1, "Close Me")
 145         self.btn.SetPosition((15, 15))
 146 
 147 
 148     def BindEvents(self):
 149         """
 150         ...
 151         """
 152 
 153         # Bind the close event to an event handler.
 154         self.Bind(wx.EVT_CLOSE, self.OnCloseWindow)
 155         self.Bind(wx.EVT_BUTTON, self.OnCloseMe, self.btn)
 156         self.Bind(wx.EVT_MENU, self.OnCloseWindow, id=wx.ID_EXIT)
 157 
 158 
 159     def DoLayout(self):
 160         """
 161         ...
 162         """
 163 
 164         # MainSizer is the top-level one that manages everything.
 165         mainSizer = wx.BoxSizer(wx.VERTICAL)
 166 
 167         mbSizer = wx.BoxSizer(wx.HORIZONTAL)
 168         mbSizer.Add(self.menuBar, 1, wx.ALL, 0)
 169 
 170         mainSizer.Add(mbSizer, 0, wx.EXPAND, 0)
 171         mainSizer.Add(self.mainPnl, 1, wx.EXPAND, 0)
 172 
 173         # Finally, tell the panel to use the sizer for layout.
 174         self.SetSizer(mainSizer)
 175         self.Layout()
 176 
 177 
 178     def OnCloseMe(self, event):
 179         """
 180         ...
 181         """
 182 
 183         self.Close(True)
 184 
 185 
 186     def OnCloseWindow(self, event):
 187         """
 188         ...
 189         """
 190 
 191         self.Destroy()
 192 
 193 #-------------------------------------------------------------------------------
 194 
 195 class MyApp(wx.App):
 196     """
 197     ....
 198     """
 199     def OnInit(self):
 200 
 201         #------------
 202 
 203         self.installDir = os.path.split(os.path.abspath(sys.argv[0]))[0]
 204 
 205         #------------
 206 
 207         frame = MyFrame(None, -1)
 208         self.SetTopWindow(frame)
 209         frame.Show(True)
 210 
 211         return True
 212 
 213     #---------------------------------------------------------------------------
 214 
 215     def GetInstallDir(self):
 216         """
 217         Returns the installation directory for my application.
 218         """
 219 
 220         return self.installDir
 221 
 222 
 223     def GetIconsDir(self):
 224         """
 225         Returns the icons directory for my application.
 226         """
 227 
 228         icons_dir = os.path.join(self.installDir, "icons")
 229         return icons_dir
 230 
 231 #-------------------------------------------------------------------------------
 232 
 233 def main():
 234     app = MyApp(False)
 235     app.MainLoop()
 236 
 237 #-------------------------------------------------------------------------------
 238 
 239 if __name__ == "__main__" :
 240     main()


Download source

source.zip


Additional Information

Link :

- - - - -

https://wiki.wxpython.org/TitleIndex

https://docs.wxpython.org/


Thanks to

Andrea Gavana, the wxPython community...


About this page

Date(d/m/y) Person (bot) Comments :

19/07/20 - Ecco (Created page for wxPython Phoenix).


Comments

- blah, blah, blah....

How to create a flat menu - AGW package (Phoenix) (last edited 2020-12-13 13:37:10 by Ecco)

NOTE: To edit pages in this wiki you must be a member of the TrustedEditorsGroup.