Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WxSlider with floating point values?

I'm searching for a possibility to use wxSlider with floating point values, but cannot find any working examples. Here a subclass is proposed but I don't get if all methods must be re-defined?

very grateful for possible indications.

like image 252
Tornasole Avatar asked Jan 17 '11 00:01

Tornasole


3 Answers

I came up with the following solution, which works fine for my purpose in Linux. (you don't need to override GetRange since it works automatically with overridden GetMin/Max.)

""" floating point slider customized from wx.Slider """


import wx


class FloatSlider(wx.Slider):

    def __init__(self, parent, id, value, minval, maxval, res,
                 size=wx.DefaultSize, style=wx.SL_HORIZONTAL,
                 name='floatslider'):
        self._value = value
        self._min = minval
        self._max = maxval
        self._res = res
        ival, imin, imax = [round(v/res) for v in (value, minval, maxval)]
        self._islider = super(FloatSlider, self)
        self._islider.__init__(
            parent, id, ival, imin, imax, size=size, style=style, name=name
        )
        self.Bind(wx.EVT_SCROLL, self._OnScroll)

    def _OnScroll(self, event):
        ival = self._islider.GetValue()
        imin = self._islider.GetMin()
        imax = self._islider.GetMax()
        if ival == imin:
            self._value = self._min
        elif ival == imax:
            self._value = self._max
        else:
            self._value = ival * self._res
        event.Skip()
        #print 'OnScroll: value=%f, ival=%d' % (self._value, ival)

    def GetValue(self):
        return self._value

    def GetMin(self):
        return self._min

    def GetMax(self):
        return self._max

    def GetRes(self):
        return self._res

    def SetValue(self, value):
        self._islider.SetValue(round(value/self._res))
        self._value = value

    def SetMin(self, minval):
        self._islider.SetMin(round(minval/self._res))
        self._min = minval

    def SetMax(self, maxval):
        self._islider.SetMax(round(maxval/self._res))
        self._max = maxval

    def SetRes(self, res):
        self._islider.SetRange(round(self._min/res), round(self._max/res))
        self._islider.SetValue(round(self._value/res))
        self._res = res

    def SetRange(self, minval, maxval):
        self._islider.SetRange(round(minval/self._res), round(maxval/self._res))
        self._min = minval
        self._max = maxval


if __name__ == '__main__':
    app = wx.PySimpleApp()
    myframe = wx.Frame(None, size=(100, 30), pos=(200, 200),
                       style=wx.FIXED_MINSIZE|wx.CLOSE_BOX)
    fslider = FloatSlider(myframe, -1, 0.2, 0.10004, 1.00008, 1e-4)
    myframe.Show()
    app.MainLoop()
like image 119
jung rhew Avatar answered Oct 14 '22 04:10

jung rhew


See the wx.lib.agw.FloatSpin class.

like image 3
Steven Sproat Avatar answered Oct 14 '22 03:10

Steven Sproat


It only modifies the returned value; all other methods should automatically derive from wx.Slider. It should work as given.

Why don't you try it and see?

like image 1
Hugh Bothwell Avatar answered Oct 14 '22 03:10

Hugh Bothwell