How to use wx.Slider with SELRANGE?

The docs for wx.Slider( wxPython for py2 , wxPython for py3 , wxWidgets ) specify a widget control with a name wx.SL_SELRANGEthat allows "the user to select a range on the slider (MSW only)." For me, this speaks of double control, two sliders on the same axis, to determine the low / high range. I can not get it to display two controls.

Basic code to run it. I don’t even care about the methods, events, or what is in this place, just to show something.

class MyFrame(wx.Frame):
    def __init__(self, *args, **kwds):
        # ... sizers and other stuff
        self.myslider = wx.Slider(self.notebook_1_pane_2, wx.ID_ANY, 0, -100, 100, style=wx.SL_SELRANGE)
        # ...
        self.myslider.SetSelection(10, 90)

However, all I managed to show is a blue line, covering where I expect it to be.

enter image description here

The wxPython docs all talk about this, but how should the user be able to "select a range on the slider" as shown here (taken from shiny)?

enter image description here

What am I missing? Are there any reasonable public examples of wxPython wx.Sliderin the wild with this functionality?

PS:

  • One page that I found speaks only about WinXP, but since this page has not been updated after seven years, I do not consider it authoritative with regard to version limitation.
  • I use wxGlade for gui layout, but I certainly wish / can go into the code after exporting and extinguishing.

System: win81_64, python-2.7.10, wxPython-3.0.2.0

+4
source share
1 answer

, . , . . left_gap right_gap , . , .

class RangeSlider(wx.Slider):
def __init__(self, left_gap, right_gap, *args, **kwargs):
    wx.Slider.__init__(self, *args, **kwargs)
    self.left_gap = left_gap
    self.right_gap = right_gap
    self.Bind(wx.EVT_LEFT_UP, self.on_left_click)
    self.Bind(wx.EVT_RIGHT_UP, self.on_right_click)
    self.Bind(wx.EVT_SCROLL_PAGEUP, self.on_pageup)
    self.Bind(wx.EVT_SCROLL_PAGEDOWN, self.on_pagedown)
    self.Bind(wx.EVT_SCROLL_THUMBTRACK, self.on_slide)

    self.slider_value=self.Value
    self.is_dragging=False

def linapp(self, x1, x2, y1, y2, x):
    proportion=float(x - x1) / (x2 - x1)
    length = y2 - y1
    return round(proportion*length + y1)

# if left click set the start of selection
def on_left_click(self, e):

    if not self.is_dragging: #if this wasn't a dragging operation
        position = self.get_position(e)
        if position <= self.SelEnd:
            self.SetSelection(position, self.SelEnd)
        else:
            self.SetSelection(self.SelEnd, position)
    else:
        self.is_dragging = False
    e.Skip()

# if right click set the end of selection
def on_right_click(self, e):
    position = self.get_position(e)
    if position >= self.SelStart:
        self.SetSelection(self.SelStart, position)
    else:
        self.SetSelection(position, self.SelStart)
    e.Skip()

# drag the selection along when sliding
def on_slide(self, e):
    self.is_dragging=True
    delta_distance=self.Value-self.slider_value
    self.SetSelection(self.SelStart+delta_distance, self.SelEnd+delta_distance)
    self.slider_value=self.Value

# disable pageup and pagedown using following functions
def on_pageup(self, e):
    self.SetValue(self.Value+self.PageSize)

def on_pagedown(self, e):
    self.SetValue(self.Value-self.PageSize)

# get click position on the slider scale
def get_position(self, e):
    click_min = self.left_gap #standard size 9
    click_max = self.GetSize()[0] - self.right_gap #standard size 55
    click_position = e.GetX()
    result_min = self.GetMin()
    result_max = self.GetMax()
    if click_position > click_min and click_position < click_max:
        result = self.linapp(click_min, click_max,
                             result_min, result_max,
                             click_position)
    elif click_position <= click_min:
        result = result_min
    else:
        result = result_max

    return result
0

All Articles