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.
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()
See the wx.lib.agw.FloatSpin class.
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With