wxPython Major Classes-wxPython Slider Class - wxPython

What is the use of wxPython Slider Class?

A slider presents the user with a groove over which a handle can be moved. It is a classic widget to control a bounded value. Position of the handle on the groove is equivalent to an integer between the lower and upper bounds of the control.

wxPython API contains wx.Slider class. It offers same functionality as that of Scrollbar. Slider offers a convenient way to handle dragging the handle by slider specific wx.EVT_SLIDER event binder.

The definition of wx.Slider constructor takes the following eight parameters

Slider’s lower and upper values are set by minValue and maxValue parameters. The starting value is defined by the value parameter.

Many style parameter values are defined. Following are some of them

Given below are the most commonly used parameters of styles.

S.N.

Parameters & Description

1

wxSL_HORIZONTAL

Horizontal slider

2

wxSL_VERTICAL

Vertical slider

3

wxSL_AUTOTICKS

Displays tickmarks on the slider

4

wxSL_LABELS

Displays the min, max, and current value

5

wxSL_MIN_MAX_LABELS

Displays the min and max value

6

wxSL_VALUE_LABEL

Displays the current value only

The useful methods of wx.Slider class are

Given below are the most commonly used methods of wx.Slider

S.N.

Methods & Description

1

GetMin()

Returns the minimum value of the slider

2

GetMax()

Returns the maximum value of the slider

3

GetValue()

Returns the current value of the slider

4

SetMin()

Sets the minimum value of the slider

5

SetMax()

Sets the maximum value of the slider

6

SetRange()

Sets the minimum and maximum slider values

7

SetValue()

Sets the current value programmatically

8

SetTick()

Displays the tick mark at the given position

9

SetTickFreq()

Sets the tick interval between the min and max values

As the slider behaves similar to a scroll bar, the scroll bar event binders can also be used along with it.

S.N.

Events & Description

1

wx.EVT_SCROLL

Processes the scroll event

2

wx.EVT_SLIDER

When the slider position changes, either by moving the handle or programmatically

Example

In the example that follows, the slider is used to control the size of a label. First of all, a slider object is placed in a vertical box sizer below which is a StaticText.

Wx.EVT_SLIDER binder is associated with OnSliderScroll() handler.

The handler itself is fetching slider’s current value and using it as font size for the label’s text.

The complete code is as follows

Run the code and try dragging the slider handle to see the label’s font size changing. The above code produces the following output

slider_output.jpg

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

wxPython Topics