python - Formatting a spinbutton's display in PyGObject/GTK+3 -
python - Formatting a spinbutton's display in PyGObject/GTK+3 -
i'm in process of porting application pygtk pygobject. it's going because did conventional things pygtk. there's 1 ugly hack using display value of spinbutton currency (with $ in front end of it).
i got solution the pygtk mailing list in days before stack overflow. can see, magic happens on input , output signals:
import gtk, ctypes def _currency_input(spinbutton, gpointer): text = spinbutton.get_text() if text.startswith('$'): text = text[1:] double = ctypes.c_double.from_address(hash(gpointer)) double.value = float(text) homecoming true def _currency_output(spinbutton): text = '$%.*f' % (int(spinbutton.props.digits), spinbutton.props.adjustment.value) spinbutton.set_text(text) homecoming true def format_spinbutton_currency(spinbutton): spinbutton.connect('input', _currency_input) spinbutton.connect('output', _currency_output) def _test(): s = gtk.spinbutton(gtk.adjustment(value=1, lower=0, upper=1000, step_incr=1)) s.props.digits = 2 format_spinbutton_currency(s) w = gtk.window() w.props.border_width = 12 w.add(s) w.show_all() w.connect('destroy', gtk.main_quit) gtk.main() if __name__ == '__main__': _test()
doing best translate pygobject, came with:
from gi.repository import gtk import ctypes def _currency_input(spinbutton, gpointer): text = spinbutton.get_text() if text.startswith('$'): text = text[1:] double = ctypes.c_double.from_address(hash(gpointer)) double.value = float(text) homecoming true def _currency_output(spinbutton): text = '$%.*f' % (int(spinbutton.props.digits), spinbutton.get_value()) spinbutton.set_text(text) homecoming true def format_spinbutton_currency(spinbutton): spinbutton.connect('input', _currency_input) spinbutton.connect('output', _currency_output) def _test(): s = gtk.spinbutton() s.set_adjustment(gtk.adjustment(value=1, lower=0, upper=1000, step_increment=1)) s.props.digits = 2 format_spinbutton_currency(s) w = gtk.window() w.props.border_width = 12 w.add(s) w.show_all() w.connect('destroy', gtk.main_quit) gtk.main() if __name__ == '__main__': _test()
unfortunately, doesn't work. shows fine initially, when click or downwards error, crashes , see:
/usr/lib/python2.7/dist-packages/gi/types.py:43: warning: g_value_get_double: assertion `g_value_holds_double (value)' failed homecoming info.invoke(*args, **kwargs) segmentation fault
any thought error message means?
or part of code might not work under pygobject?
or, improve yet, how prepare error?
or, improve still, more straightforward solution original problem (displaying $ in front end of spinbutton contents)?
from pygtk documentation:
http://developer.gnome.org/pygtk/stable/class-gtkspinbutton.html#signal-gtkspinbutton--input
the "input" signal emitted when value changes. value_ptr gpointer value cannot accessed pygtk. this signal cannot handled in pygtk.
so, i'm atonished see like:
double = ctypes.c_double.from_address(hash(gpointer))
this real hack, got , awful error "segmentation fault" means messing in memory don't have to, , it's quite generic, happens illustration when in c seek manually access memory pointer not handled application.
this hard one, tried 1 hr , approaches tried had problems. know not , answer, workaround , if need currency symbol (not grouping) can experiment adding currency symbol image inherited gtk.entry set_icon_from_pixbuf():
(obviously set image currency image)
kind regards
python gtk pygtk gtk3 pygobject
Comments
Post a Comment