Adding a menubar toolbar or status bar to a frame

Often, an application window will have one or more of three special subwindows—a menubar at the top, a toolbar below that, and a status bar at the bottom. This is so common that wxPython provides special shortcut methods for the creation of toolbars and status bars (and menus aren't that hard either). Figure 2.9 shows a sample frame with a menubar, toolbar, and status bar.

Listing 2.4 shows the_init_method which decorates a plain window with all three subwindows.

Again, this is just an overview, and the classes in question will be covered in more detail in chapter 7 and later in part 2.

_i Toolbars

Copy

Cut Paste

Options...

Copy in status bar

Figure 2.9 A sample frame with menubar, toolbar, and status bar

Listing 2.4 Creating toolbars

#!/usr/bin/env python import wx import images class ToolbarFrame(wx.Frame)

'Toolbars'

A Creating the status bar def _init_(self, parent, id):

wx.Frame._init__(self, parent, id, size=(300, 200)) panel = wx.Panel(self) panel.SetBackgroundColour('White') statusBar = self.CreateStatusBar() toolbar = self.CreateToolBar() toolbar.AddSimpleTool(wx.NewId(), images.getNewBitmap(

"New", "Long help for 'New'") <-

A Creating the toolbar d Adding a tool to the bar

Preparing the toolbar for display if

"&Copy" "C&ut", "Paste", "

"&Options.

menul = wx.Menu() menuBar.Append(menu1, menu2 = wx.Menu() menu2.Append(wx.NewId() menu2.Append(wx.NewId() menu2.Append(wx.NewId() menu2.AppendSeparator() menu2.Append(wx.NewId() menuBar.Append(menu2, "&Edit") self.SetMenuBar(menuBar) <1—

Attaching

_name_ == '__main__': the menubar app = wx.PySimpleApp() to the frame frame = ToolbarFrame(parent=None, id= frame.Show() app.MainLoop()

I f Creatii y indivic

C re ati ng two individual menus

in status bar")

Creating individual m enu it e m s

Attaching the menu to the menubar

"Display Options")

O This line creates a status bar, which is an instance of the class wx.StatusBar. As far as the frame is concerned, it's a subwindow placed at the bottom of the frame whose width is the same as the width of the frame, and whose height is determined by the underlying operating system. The purpose of the status bar is to display text set by various events in the application. The text size and font of the status window are also set by the underlying system.

© Creates an instance of wx.ToolBar, which is a container of command buttons automatically positioned at the top of the frame.

d There are two methods to add tools to your toolbar, this line shows the one with fewer arguments, AddSimpleTool(). The arguments are an ID, a bitmap, short help text to display as a tooltip for the item, and longer help text to be displayed in the status bar for the item. (Don't worry about where the bitmap is coming from at the moment.) Although the tool as displayed on the toolbar will look like a button, in terms of the event system, it behaves more like a menu item, which makes it easy for a tool to act as an exact duplicate for an item in your menubar. Specifically, pressing the tool button triggers the same kind of wxPython event as the menu item, meaning that both can be responded to by the same method.

O The Realize() method tells the toolbar to calculate where the buttons should be positioned, and is required if the toolbar has any tools added that depend on the toolbar's dynamic positioning because they do not specify their own size or position explictly.

f Creates a wx.Menu object, which represents a single menu on the bar. (We created two of them, but only so that we could take a screen shot of the pull-down without covering the toolbar icon.)

g Creates the individual menu items, with the arguments representing an ID, the text of the item, and the text displayed on the status bar when the menu is moused over. The "&" indicates the character to be chosen for a menu accelerator.

In addition to using widgets in your frames, you'll also communicate with users via standard dialogs, which is made easy using wxPython's predefined dialog functions.

Was this article helpful?

0 -1

Post a comment