Naresh
Naresh

Reputation: 21

how to change display in gtk.spinbutton

I have a gtk.spinbutton and I want to set the digits according to the locale format. like say I have a locale installed hungarian so my decimal separator is '.'(dot) and thousand separator is ','(comma) eg: my spinbutton value is 1131191 so after the user focus out of the gtk.spinbutton my value should convert to 11,311.91 . the conversion is made by me but I am not able to set it to gtk.spinbutton either using set_text / set_value method.

Any help is appreciated !

Thanks

Upvotes: 2

Views: 1315

Answers (1)

Johannes Sasongko
Johannes Sasongko

Reputation: 4238

Formatting a SpinButton can be done by handling the output signal.

locale.setlocale(locale.LC_ALL, '')

def output(spin):
    digits = int(spin.props.digits)
    value = spin.props.value
    text = locale.format('%.*f', (digits, value), True)
    spin.props.text = text
    return True

spin.connect('output', output)

If you also want to let users enter values in the localised format (e.g. let the user type "1,000" instead of "1000"), handle the input signal.

def input(spin, new_value):
    text = spin.props.text
    try:
        value = locale.atof(text)
    except ValueError:
        return -1
    p = ctypes.c_double.from_address(hash(new_value))
    p.value = value
    return True

spin.connect('input', input)

(This code is longer than it should be because PyGTK does not properly wrap input, hence the ctypes hack. It's just parsing the text and then assigning the numeric value to a pointer location.)

Credits: The ctypes hack and digits formatting are inspired by Tim Evans's post in the PyGTK mailing list.

Upvotes: 3

Related Questions