display leading zero in a gtk.SpinButton

允我心安 提交于 2019-12-07 04:37:49

问题


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.


回答1:


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)


来源:https://stackoverflow.com/questions/9998713/display-leading-zero-in-a-gtk-spinbutton

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!