Search code examples
callbackpygtktext-formatting

pygtk - spinbutton: output-signal


I use a spinbutton in my application to let the user choose a number between -1 and 100. In my application -1 means Infinity. So I want to show the text "Infinity", if the user selects the value -1. This is my code:

def spin_output(spin):
    digits = int(spin.props.digits)
    value = spin.props.value
    if value < 0:
        spin.props.text = "Infinity"  # u"\u221E"
    else:
        spin.props.text = '{0:.{1}f}'.format(value, digits)
    return True
self.my_spin.connect('output', spin_output)

When the "Infinity"-value is selected and the user presses the "up"-button the value changes to 100 instead of 0. When i replace "Infinity" with u"\u221E" and the user presses the "up"-button while it is selected, the value changes to 1.

What I want is, that the user can select the values in that order: Infinity, 0, 1, ... What is my mistake?

I thought that only the underlying adjustment is changed when the user changes the value and my function is only used to show the current value.


Solution

  • Ok, i found a solution. I wrote the counterpart for the output-signal-handler ... the input-signal-handler :-)

    def parallel_spin_input(spin, new_value):
        text = spin.get_text()
        if text == u"\u221E":
            value = -1
        else:
            try:
                value = float(text)
            except ValueError:
                return -1
        p = ctypes.c_double.from_address(hash(new_value))
        p.value = value
        return True
    self.parallel_spin.connect('input', parallel_spin_input)
    

    This seems to work well ;-)