29a.ch by Jonas Wagner

Stepped scales with pygtk

The Scale Widgets that come with gtk ignore step_incr from the adjustment when the user drags the slider. So there is no way to specify that you only want to use increments of 0.5 or 1.0. So here's how to do it:

class Scale(object):
    """A scale that adheres to increment steps"""
    def __init__(self):
        self.connect("change-value", self.adjust)

    def adjust(self, range, scroll, value):
        adj = self.get_adjustment()
        lower = adj.get_property('lower')
        upper = adj.get_property('upper')
        incr = adj.get_property('step-increment')
        value -= (value % incr)
        self.set_value(min(max(lower, value), upper))
        return True

class VScale(gtk.VScale, Scale):
    def __init__(self, *args):
        gtk.VScale.__init__(self, *args)

class HScale(gtk.HScale, Scale):
    def __init__(self, *args):
        gtk.HScale.__init__(self, *args)
comments powered by Disqus