6

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.

jkd
  • 1,327
  • 14
  • 29

1 Answers1

8

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)
ptomato
  • 56,175
  • 13
  • 112
  • 165
  • thanks a lot, just had to change spin_button.set_text('{:02d}'.format(adjustment.get_value())) to spin_button.set_text('{:02d}'.format(int(adjustment.get_value()))) in the function you gave. I saw a similar code in the gtk documentation (for C) but i couldn't find the .set_text() method in pygtk (i guess gtk.SpinButton has gtk.Entry as ancestor or something like that). – jkd Apr 03 '12 at 18:51
  • I corrected the code. You are right, spin button inherits from entry. – ptomato Apr 03 '12 at 21:43