Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

display leading zero in a gtk.SpinButton

Tags:

pygtk

gtk

i'd like to have a leading zero in a spinbutton in order to always have two digits displayed.

adj_hour = gtk.Adjustment(int(time.strftime("%H")),0,24,1,1)
entry_hour = gtk.SpinButton()
entry_hour.set_adjustment(adj_hour)

problem is that gtk.Adjustment's first argument has to be float/int.

i tried things like:

adj_hour = gtk.Adjustment(float(format(int(time.strftime("%H")), '02d')),0,24,1,1)

but it doesn't work.

like image 662
jkd Avatar asked Apr 03 '12 17:04

jkd


1 Answers

Connect to the output signal of the spin button. For example, adapting the C code in the documentation I linked to:

def show_leading_zeros(spin_button):
    adjustment = spin_button.get_adjustment()
    spin_button.set_text('{:02d}'.format(int(adjustment.get_value())))
    return True

...

entry_hour.connect('output', show_leading_zeros)
like image 98
ptomato Avatar answered Sep 30 '22 18:09

ptomato