Hi guys,
I’m trying to make a wx.Slider with steps. That is, when the user moves the “pointer”, it only moves accordingly to the step value. I’m only working with steps that are multiple of the min and max value, but you can make that work if you want.
Well, here’s my code so far. When using the arrow keys, works OK, but not for the mouse.
Thank you!
import wx
class Mywin(wx.Frame):
def __init__(self, parent, title):
super(Mywin, self).__init__(parent, title = title, size=(250,150))
self.min = 0
self.lastValue = self.min
self.max = 100
self.step = 25
self.InitUI()
self.CenterOnScreen()
def InitUI(self):
vbox = wx.BoxSizer(wx.VERTICAL)
self.sld = wx.Slider(self, value=self.min, minValue=self.min, maxValue=self.max,
style = wx.SL_HORIZONTAL|wx.SL_LABELS)
vbox.Add(self.sld, 1, flag = wx.EXPAND | wx.TOP, border=20)
self.sld.Bind(wx.EVT_SLIDER, self.OnSliderScroll)
self.SetSizer(vbox)
def OnSliderScroll(self, e):
obj = e.GetEventObject()
val = obj.GetValue()
if val >= self.max or val <= self.min:
return
if val > self.lastValue:
newValue = self.lastValue + self.step
self.sld.SetValue(newValue)
else:
newValue = self.lastValue - self.step
self.sld.SetValue(newValue)
self.lastValue = newValue
ex = wx.App()
Mywin(None, 'Slider demo').Show()
ex.MainLoop()