How do I add and update a status bar

In wxPython, you can add and place a status bar in the bottom of a frame by calling the frame's CreateStatusBar() method. The status bar automatically resizes itself when the parent frame resizes. By default, the status bar is an instance of the class wx.StatusBar. To create a custom status bar subclass, attach it to your frame using the SetStatusBar() method, with an instance of your new class as the argument.

To display a single piece of text in your status bar, you can use the SetStatus-Text() method of wx.StatusBar. Listing 6.2 extends the SketchFrame class illustrated in listing 6.1 to display the current mouse pointer position in the status bar.

Listing 6.2 Adding a simple status bar to the frame import wx from examplel import SketchWindow class SketchFrame(wx.Frame):

wx.Frame._init_(self, parent, -1, "Sketch Frame", size=(800,600)) self.sketch = SketchWindow(self, -1)

self.sketch.Bind(wx.EVT_MOTION, self.OnSketchMotion) self.statusbar = self.CreateStatusBar()

def OnSketchMotion(self, event):

self.statusbar.SetStatusText(str(event.GetPositionTuple())) event.Skip()

app = wx.PySimpleApp() frame = SketchFrame(None) frame.Show(True) app.MainLoop()

We've hooked up the status bar by having the frame also capture the wx.evt_motion event of the sketch window. The event handler sets the status text using the data provided by the event. Then it calls Skip() to ensure that the other OnMotion() method is called, otherwise the line won't be drawn.

You can treat the status bar like any other widget by adding objects to it. As a shortcut, if you want to display more than one text element, you can create multiple status text fields in the status bar. To use this functionality, call the method SetFieldsCount() with the number of fields you want; the default, as we've seen, is one. After that, use SetStatusText() as before, but with a second argument specifying the field being set by the method. The field numbers start at zero. If you don't specify a field, the zero field is set by default, which is why the previous example works even though we didn't specify the field.

By default, each of the fields have the same width. However that's not always what you want. To adjust the sizes of the text fields, wxPython provides the method

SetStatusWidth(). The method takes a Python list of integers, which must be the same length as the number of fields in the status bar. The integer list is used to calculate the width of the fields in order. If the integer is positive, it is the absolute fixed width of the field. If you want the field width to change with the frame, indicate that by using a negative integer. The absolute value of the negative integer indicates the relative size of the field; think of it as the number of shares of the total width that field gets. For example, the call statusbar.SetStatusWidth([-1, -2, -3]) results in the rightmost field getting half the width (3 parts out of 6), the center field getting a third of the width (2 parts out of 6), and the leftmost field getting a sixth of the width (1 part out of 6). Figure 6.2 displays the results.

Figure 6.2 A sample status bar with the fields getting 1/6, 2/3, and 1/2 of the total width

Listing 6.3 adds support for two more status fields, one which shows the number of points in the current line being drawn, the other shows the number of lines in the current sketch. This listing produces the status bar displayed in figure 6.2.

Listing 6.3 Support for multiple status fields import wx from example1 import SketchWindow class SketchFrame(wx.Frame):

wx.Frame._init_(self, parent, -1, "Sketch Frame", size=(800,600)) self.sketch = SketchWindow(self, -1)

self.sketch.Bind(wx.EVT_MOTION, self.OnSketchMotion) self.statusbar = self.CreateStatusBar() self.statusbar.SetFieldsCount(3) self.statusbar.SetStatusWidths([-1, -2, -3])

def OnSketchMotion(self, event):

self.statusbar.SetStatusText("Pos: %s" % str(event.GetPositionTuple()), 0) self.statusbar.SetStatusText("Current Pts: %s" %

len(self.sketch.curLine), 1) self.statusbar.SetStatusText("Line Count: %s" %

len(self.sketch.lines), 2) event.Skip()

app = wx.PySimpleApp()

frame = SketchFrame(None)

frame.Show(True)

app.MainLoop()

The StatusBar class allows you to treat the status fields as a last in/first out stack. Although not useful for the demo application in this chapter, the PushStatus-Text() and PopStatusText() methods allow you to return to the previous status text after temporarily displaying new text. Both of these methods take an optional field number, so they can be used in the case of multiple status fields.

Table 6.3 summarizes the most commonly used methods of wx.StatusBar.

Table 6.3 Methods of wx.StatusBar

Function

Description

GetFieldsCount() SetFieldsCount(count)

Property for the number of fields in the status bar

GetStatusText(field=0) SetStatusText(text, field=0)

Property for the text displayed in the specified status field. The index of 0 is the default and represents the leftmost field

PopStatusText(field=0)

Pops the text statck of the specified status field, changing the text of that field to the popped value

PushStatusText(text, field=0)

Changes the display of the specified status field to the given text, and pushes that value to the top of the stack for that field

SetStatusWidths(widths)

Takes a Python list of integers and specifies the width of the status fields. A positive number indicates a fixed width in pixels, and a negative number indicates a dynamic share of the width proportional to the absolute value of the number.

In chapter 10, we'll provide more details about status bars. In the meantime, we'll discuss menus.

Was this article helpful?

+1 -1

Responses

  • Oliver
    How to create statusbar in wxpython?
    9 months ago
  • lois keene
    How to set font for statusbar in wxpython 3?
    3 months ago

Post a comment