How do I make a slider

A slider is a widget that allows the user to select a number from within a range by dragging a marker across the width or height of the control. In wxPython, the control class is wx.Slider, which includes a read-only text display of the current value of the slider. Figure 7.7 displays examples of a vertical and horizontal slider.

Basic slider use is fairly straightforward, but there are a number of events you can add.

Tkinter Slider
Figure 7.7 A vertical wx.Slider and a horizontal wx.Slider, which use the style flag wx.SL LABELS

How to use a slider

As displayed in listing 7.7, a slider can manage a single value through the control.

Listing 7.7 Displays code for the horizontal and vertical slider import wx class SliderFrame(wx.Frame):

wx.Frame._init_(self, None, -1, 'Slider Example', size=(34 0, 320)) panel = wx.Panel(self, -1) self.count = 0

slider = wx.Slider(panel, 100, 25, 1, 100, pos=(10, 10), size=(250, -1), style=wx.SL_HORIZONTAL | wx.SL_AUTOTICKS | wx.SL_LABELS ) slider.SetTickFreq(5, 1)

slider = wx.Slider(panel, 100, 25, 1, 100, pos=(125, 50), size=(-1, 250), style=wx.SL_VERTICAL | wx.SL_AUTOTICKS | wx.SL_LABELS ) slider.SetTickFreq(20, 1)

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

Typically, when you use the wx.Slider class, all you'll need is the constructor, which differs from the other calls as in the following:

wx.Slider(parent, id, value, minValue, maxValue, pos=wxDefaultPosition, size=wx.DefaultSize, style=wx.SL_HORIZONTAL, validator=wx.DefaultValidator, name="slider")

The value is the starting value of the slider, while minValue and maxValue are the extreme values.

Working with slider styles

The styles for a slider govern the placement and orientation of the slider, as listed in table 7.9.

Table 7.9 Styles for wx.Slider

Style

Description

wx.SL_AUTOTICKS

If set, the slider will display tick marks across it. The spacing is governed by the setter method SetTickFreq.

wx.SL_HORIZONTAL

The slider will be horizontal. This is the default.

wx.SL_LABELS

If set, the slider will display labels for the minimum and maximum value, and a read-only display of the current value. The current value might not display on all platforms.

wx.SL_LEFT

For a vertical slider, the ticks will be on the left of the slider.

wx.SL_RIGHT

For a vertical slider, the ticks will be on the right of the slider.

wx.SL_TOP

For a horizontal slider, the ticks will be on top of the slider.

wx.SL_VERTICAL

The slider will be vertical.

If you want changes in the slider value to affect another part of your application, there are several events you can use. These events are identical to those emitted by a window scroll bar, and are described in detail in the scrolling section of chapter 8.

Table 7.10 lists the setter properties you can apply to a slider. Each setter has an associated Get method—the descriptions in the table refer to the setter only.

Table 7.10 Settable attributes of a slider

Function

SetRange(minValue, maxValue)

Sets the boundary range of the slider

GetTickFreq() SetTickFreq(n, pos)

Sets the spacing between ticks, using the n argument. The pos argument is not actually used, but it's still required. Set it to 1.

GetLineSizeO SetLineSize(lineSize)

Sets the amount by which the value changes if you adjust the slider by one line by pressing an arrow key.

Table 7.10 Settable attributes of a slider (continued)

Function

Description

GetPageSize() SetPageSize(pageSize)

Sets the amount by which the value changes if you adjust the slider by one page by pressing page up or page down.

GetValue() GetValue(value)

Sets the value of the slider.

Although sliders provide a quick visual representation of where the value lies along the possible range, they also have a couple of weaknesses. They take up a lot of space in their primary dimension, and it's difficult to set the slider exactly using a mouse, particularly if the range is quite large, or if the user has an accessibility issue. The spinner, which we'll discuss in the next section, resolves both of these issues.

Was this article helpful?

0 0

Responses

  • ROBERT
    How to show ticks of wx slider?
    4 years ago

Post a comment