El jue, 10-01-2008 a las 00:23 +0100, Eike Nicklas escribió:
> Hi all,
>
> I am currently learning PyGTK and got stuck on the following problem:
>
> I have myentry = gtk.Entry() containing an integer (actually a string,
> of course, but int(myentry.get_text()) works) and want to increase this
> integer by one if the button '+' is pressed.
>
> Here's what I tried:
>
> myentry.connect('key-press-event', increase)
>
> def increase(entry, event):
> if event.keyval == gtk.keysyms.plus:
> mynumber = int(entry.get_text())
> mynumber += 1
> entry.set_text(str(mynumber))
>
> This works at first, but the '+' is inserted into the text entry after
> the number was increased. Do you have an idea how to prevent this?
>
> A related problem is covered in the FAQ at
>
> http://faq.pygtk.org/index.py?req=show&file=faq14.005.htp
>
> but I was not able to modify this (outdated ?) code to my needs :-(
Yep. The sample code looks outdated.
Attached the sample fixed. It would help you to do what you want.
--
Germán Poó Caamaño
http://www.ubiobio.cl/~gpoo/
Concepción - Chile
#!/usr/bin/python
import pygtk
import gtk
import gobject
def insert_cb(widget, text, length, *args):
# if you don't do this, garbage comes in with text
text = text[:length]
pos = widget.get_position()
# stop default emission
widget.emit_stop_by_name("insert_text")
gobject.idle_add(insert, widget, text, pos)
def insert(widget, text, pos):
# the next three lines set up the text. this is done because we
# can't use insert_text(): it always inserts at position zero.
orig_text = widget.get_text()
text = str.replace(text, " ", "<SPACE>")
new_text = orig_text[:pos] + text + orig_text[pos:]
# avoid recursive calls triggered by set_text
widget.handler_block(insert_sig)
# replace the text with some new text
widget.set_text(new_text)
widget.handler_unblock(insert_sig)
# set the correct position in the widget
widget.set_position(pos + len(text))
if __name__ == "__main__":
dialog = gtk.Dialog('Foo', None,
gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT,
(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL,
gtk.STOCK_OK, gtk.RESPONSE_OK))
entry = gtk.Entry()
insert_sig = entry.connect('insert_text', insert_cb)
entry.show()
dialog.vbox.pack_start(entry)
dialog.run()
_______________________________________________
pygtk mailing list [email protected]
http://www.daa.com.au/mailman/listinfo/pygtk
Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/