How can I get those neat updown arrow buttons

A spinner is a combination text control and pair of arrow buttons that adjust a numeric value, and is a great alternative to a slider when you have minimal screen space. Figure 7.8 displays a wxPython spinner control.

In wxPython, the wx.SpinCtrl class manages both the spinner buttons and the associated text display. In the next section, we'll create a spinner.

Figure 7.8 A spinner control in wxPython

Figure 7.8 A spinner control in wxPython

How to create a spinner

Use wx.SpinCtrl to change the value either by pressing the buttons or by typing in the text control. Non-numeric text typed into the control is ignored, although the control doesn't change back to the previous value until a button is pressed. A numeric value outside the control range is treated as the relevant maximum or minimum value, although that value doesn't revert to the end of the range until you press a button. Listing 7.8 displays the use of wx.SpinCtrl.

Listing 7.8 Using wx.SpinCtrl import wx class SpinnerFrame(wx.Frame):

wx.Frame._init_(self, None, -1, 'Spinner Example', size=(100, 100))

sc = wx.SpinCtrl(panel, -1, "", (30, 20), (80, -1))

sc.SetRange(1,100)

sc.SetValue(5)

if __name__ == '__main__': app = wx.PySimpleApp() SpinnerFrame().Show() app.MainLoop()

Nearly all of the complexity of the spin control is in the constructor, which has several arguments as in the following:

wx.SpinCtrl(parent, id=-1, value=wx.EmptyString, pos=wx.DefaultPosition, size=wx.DefaultSize, style=wx.SP_ARROW_KEYS, min=0, max=100, initial=0, name="wxSpinCtrl")

The first part of this constructor is similar to all the other wx.Window constructors. However, the value argument here is a dummy. Set the initial value of the control using the initial argument, and use the min and max arguments to set the range of the control.

There are two style flags for wx.SpinCtrl. By default, wx.SP_ARROW_KEYS is declared because it allows the user to change the value of the control from the keyboard using the up and down arrow keys. The wx.SP_WRAP style causes the value of the control to wrap, meaning that if you go off the edge at one extreme, you wind up at the other extreme. Also, you can catch the evt_spinctrl event, which is generated whenever the spin value is changed (even if it is changed via text entry). If the text is changed, an evt_text is fired, just as it would be if you were using a standalone text control.

As displayed in listing 7.8, you can set the range and value using the Set-Range(minVal, maxVal) and SetValue(value) methods. The SetValue() function can take either a string or an integer. To get the values, use the methods GetValue() (which returns an integer), GetMin() , and GetMax() .

When you need more control over the behavior of the spinner, such as spinning through floating point values, or a list of strings, you can put a wx.Spin-Button together with a wx.TextCtrl, and build plumbing between them. Put the two controls next to each other and catch EVT_SPIN events from the wx.Spin-Button, updating the value in the wx.TextCtrl.

+1 -2

Post a comment