def _eventValue(self, ev): "Determine a numerical value from the event coordinates" dim = tall(*self.size) wh = self.knob.size[dim] + 2 x = self._round((self.relXY(ev.pos)[dim] - wh / 2) / (self.size[dim] - wh)) if self._flip: x = 1 - x return self._calc(x)
def __init__(self, size=(128,16), color="grey", lower=0, upper=1): super().__init__(size) cfg = dict(anchor=BOTTOMLEFT, pos=(0, size[1]-1)) if tall(*size) else dict(anchor=TOPLEFT) self += Image(bg=color).config(**cfg) self.lower = lower self.upper = upper self.value = lower
def __init__(self, size=(128, 16), color="grey", lower=0, upper=1): super().__init__(size) cfg = dict(anchor=BOTTOMLEFT, pos=(0, size[1] - 1)) if tall( *size) else dict(anchor=TOPLEFT) self += Image(bg=color).config(**cfg) self.lower = lower self.upper = upper self.value = lower
def value(self, val): "Change the current value of the progress bar" val = max(self.lower, min(self.upper, val)) self._val = val dim = tall(*self.size) x = (val - self.lower) / (self.upper - self.lower) x = max(1, round(x * self.size[dim])) size = (self.width, x) if dim else (x, self.height) self[0].config(size=size)
def val(self, val): "Change the current value of the slider" val = max(self.lower, min(self.upper, val)) x = self._round((val - self.lower) / (self.upper - self.lower)) self._val = self._calc(x) if self._flip: x = 1 - x knob = self.knob dim = tall(*self.size) wh = knob.size[dim] + 2 w, h = self.size knob.pos = (w / 2, wh / 2 + (h - wh) * x) if dim else (wh / 2 + (w - wh) * x, h / 2)
def onkeydown(self, ev): "Handle arrow keys when the slider is focussed" if tall(*self.size): dx = 1 if ev.key == K_DOWN else -1 if ev.key == K_UP else 0 else: dx = 1 if ev.key == K_RIGHT else -1 if ev.key == K_LEFT else 0 if self._flip: dx = -dx if dx: cur = self._val s = self.steps if not s: s = 100 self.val = self._val + dx * (self.upper - self.lower) / s if self._val != cur: setattr(ev, "method", KEY) self.bubble("onchange", ev)
def onclick(self, ev): "Handle click events on the slider canvas" self._lastButton = btn = ev.button if btn in self.allowButton: dim = tall(*self.size) lims = [self.lower, self.upper] if btn == 4: x = lims[dim] elif btn == 5: x = lims[1-dim] else: x = self._eventValue(ev) s = self.steps if not s: s = 100 s = (self.upper - self.lower) / s v = self._val if x != v: if x < v: self.val -= s else: self.val += s setattr(ev, "method", SCROLL if btn in (4,5) else CLICK) if ev: self.bubble("onchange", ev)
def onclick(self, ev): "Handle click events on the slider canvas" self._lastButton = btn = ev.button if btn in self.allowButton: dim = tall(*self.size) lims = [self.lower, self.upper] if btn == 4: x = lims[dim] elif btn == 5: x = lims[1-dim] else: x = self._eventValue(ev) s = self.steps if not s: s = 100 s = (self.upper - self.lower) / s v = self._val if x != v: if btn == 1 and ev.target is self: self.val = x elif x < v: self.val -= s else: self.val += s setattr(ev, "method", SCROLL if btn in (4,5) else CLICK) if ev: self.bubble("onchange", ev)