How can I create fancier menus

If ordinary menu items aren't interesting enough for you, you can add a custom bitmap to be displayed next to the menu item (or used as a custom check symbol). Under MS Windows, you can also adjust the font and color of the menu item. Figure 10.7 displays a fancy menu example.

Listing 10.10 displays the code to produce the menu. To determine whether the program is running under Windows, you can check that 'wxMSW' is in the wx.PlatformInfo tuple.

Figure 10.7 Menu items with custom bitmaps, colors, and fonts

Listing 10.10 Sample code for fancier menu items import wx class MyFrame(wx.Frame):

wx.Frame. init

(self, None, -1, "Fancier Menu Example")

bmp = wx.Bitmap("open.png", wx.BITMAP_TYPE_PNG) item = wx.MenuItem(menu, -1, "Has Open Bitmap") item.SetBitmap(bmp) menu.AppendItem(item)

< | Adding a custom bitmap if 'wxMSW' in wx.PlatformInfo:

font = wx.SystemSettings.GetFont(

wx.SYS_DEFAULT_GUI_FONT) font.SetWeight(wx.BOLD)

item = wx.MenuItem(menu, -1, "Has Bold Font")

item.SetFont(font) <—i „. . .. , . „ / ^ > Changing the font menu.AppendItem(item)

item = wx.MenuItem(menu, -1, "Has Red Text") item.SetTextColour("red") menu.AppendItem(item)

^ | Changing the text color menu.AppendSeparator()

self.Bind(wx.EVT MENU, self.OnExit, exit)

menuBar = wx.MenuBar() menuBar.Append(menu, "Menu") self.SetMenuBar(menuBar)

def OnExit(self, event): self.Close()

if __name__ == "__main__": app = wx.PySimpleApp() frame = MyFrame() frame.Show() app.MainLoop()

Adding color or style to a menu item is primarily a matter of manipulating the properties that control the display. The only property that is applicable on a platform other than Windows is the bitmap, managed by GetBitmap(), that returns an item of type wx.Bitmap. There are two setters. The first, which works on all platforms, is SetBitmap(bmp). This sets a bitmap displayed next to the menu item at all times. If you are on MS Windows and want to set custom bitmaps for a toggle menu, you can use SetBitmaps(checked, unchecked=wx.NullBitmap), which creates one bitmap to be displayed when the item is checked, and one when it is unchecked. If the menu item is not a toggle, the checked argument is unused.

Under MS Windows, there are three other properties that you can change to affect menu item appearance, as listed in table 10.7. We recommend that you use these with caution, and only in cases where they would clearly enhance the user experience.

Table 10.7 Menu item appearance properties

Methods

Description

GetBackgroundColour() SetBackgroundColour(colour)

The property type is wx.Colour, the setter can also be passed a string which is the name of a wxPython color. Manages the background color of the item.

GetFont() SetFont(font)

The display font of the item. Type is wx.Font.

GetTextColour() SetTextColour(colour)

Manages the color of the text in the item display. Type as in background color.

Now that we've covered the functional aspects of using menus, we'll close the chapter with a brief discussion of how to use menus well and how to make your application easier for your users.

Was this article helpful?

0 0

Responses

  • jan
    How to make fancy menu in python?
    8 years ago

Post a comment