[pygtk] Formatting a spinbutton's display

Jeremy S dumbmatter at gmail.com
Wed Jul 30 13:38:52 WST 2008


Thanks for the tip.  I made a little progress, but it's still not
working correctly.  If I press the up button once, it goes from $0 to
$1 correctly.  But if I press it again, sb_value_changed gets called
twice: once to set it back to $0 and then once again to increment it
to $1.  I'm not really sure why.  And if I'm at $0 and I press down,
it changes to 0 and sb_value_changed is never called.  I feel like I'm
missing something important in my approach.  Any ideas?

import gtk

def sb_value_changed(spin_button):
    print 'value_changed', spin_button.get_text(), spin_button.get_value(),

    current_text = spin_button.get_value()
    spin_button.set_text('$%d' % current_text)
    print spin_button.get_text()

if __name__ == "__main__":
    window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    window.set_default_size(100, 100)
    window.connect("destroy", gtk.main_quit)

    adj = gtk.Adjustment(0, 0, 10, 1, 10)
    spin_button = gtk.SpinButton(adj, 0, 0)
    spin_button.set_numeric(False)
    spin_button.connect("value_changed", sb_value_changed)

    window.add(spin_button)
    window.show_all()

    sb_value_changed(spin_button)

    gtk.main()

On Mon, Jul 28, 2008 at 10:17 AM, Walter Leibbrandt
<walter at translate.org.za> wrote:
> IIRC, you can set custom text with the set_text() method (or by modifying
> the "text" property), seeing as a SpinButton is a sub-class of gtk.Entry. So
> connect to the spin button's "changed" and/or "change-value" event(s), read
> and format the new text and call set_text() with your formatted string.
>
> This might also mean that you need to do set_numeric(False) as to allow
> non-numeric strings.
>
> Jeremy S wrote:
>>
>> I would like to use a spinbutton but instead of just displaying a
>> float or integer, I want the display to be formatted.  In my case I
>> want it to be formatted as currency ($ in front and commas separating
>> thousands, millions, etc.), but I can't even find any information on
>> how to tackle this problem generally.
>>
>> I know for treeviews this type of thing can be accomplished with
>> set_cell_data_func.  Is there anything similar for spinbuttons?
>> _______________________________________________
>> pygtk mailing list   pygtk at daa.com.au
>> http://www.daa.com.au/mailman/listinfo/pygtk
>> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>>
>
> --
> Walter Leibbrandt                  http://translate.org.za/blogs/walter
> Software Developer                                  +27 12 460 1095 (w)
> Translate.org.za
>
> Recent blogs:
> * Gtk+ Tip of the Day: Modifying the subject of an event being processed
> http://www.translate.org.za/blogs/walter/en/content/gtk-tip-day-modifying-subject-event-being-processed
> * Spelt 0.1rc2 released
> * CorpusCatcher 0.1 released
>
>
>


More information about the pygtk mailing list