def METHOD_NAME(self, widget):
    try:
        val = int(widget.get_text())
    except ValueError:
        val = -1
    if val < 0:
        val = 0
        widget.set_text(str(val))
    elif val >= len(self.img.sprites):
        val = len(self.img.sprites) - 1
        widget.set_text(str(val))
    self._switch_entry()