[pygtk] Deselecting the text from an Entry the first time it appears.

Timo timomlists at gmail.com
Mon Nov 22 04:33:19 WST 2010


On 21-11-10 14:57, Fabrice Delente wrote:
>
> Hello.
>
>
> I have a DrawingArea that responds to keyboard event. I want that, 
> when I press 1-9, an Entry pops up, in which I can type a number whose 
> first digit is the digit that made the Entry pop up.
>
>
> I have nearly succeeded, however when the Entry pops up the digit is 
> selected, so that when I type the rest of the number, my first digit 
> is lost.
>
You have to set the position *after* you show the entry.

         self.interfacePourNombres.show_all()
         self.zonePourNombres.set_position(1) # Or use -1 to always put 
the cursor at the end

Cheers,
Timo

>
> Any hint? Thanks!
>
>
> Here is my code:
>
>
> import gtk
> class PyApp(gtk.Window):
>
>     def __init__(self):
>         super(PyApp, self).__init__()
>         self.set_title("Simple drawing")
>         self.resize(230, 150)
>         self.set_position(gtk.WIN_POS_CENTER)
>         self.connect("destroy", gtk.main_quit)
>
>         darea = gtk.DrawingArea()
>         darea.connect("expose-event", self.expose)
>         darea.connect("key-press-event", self.toucheEnfoncee)
>         darea.add_events(gtk.gdk.BUTTON_PRESS_MASK | 
> gtk.gdk.BUTTON_RELEASE_MASK |
>                     gtk.gdk.POINTER_MOTION_MASK | 
> gtk.gdk.KEY_PRESS_MASK | gtk.gdk.KEY_RELEASE_MASK)
>         darea.set_flags(gtk.CAN_FOCUS)
>         self.add(darea)
>
>         self.show_all()
>
>         self.interfacePourNombres = gtk.Window()
>         self.interfacePourNombres.set_modal(True)
>         self.zonePourNombres = gtk.Entry(3)
>         self.zonePourNombres.set_flags(gtk.CAN_DEFAULT)
>         self.zonePourNombres.grab_default()
>         self.zonePourNombres.select_region(0,0)
>         self.zonePourNombres.set_activates_default(True)
>         self.zonePourNombres.set_width_chars(5)
>         self.zonePourNombres.set_position(1)
>         self.zonePourNombres.size_allocate((0, 0, 100, 20))
>         self.zonePourNombres.connect("activate", self.obtenirLeNombre)
>         self.zonePourNombres.show()
>         self.interfacePourNombres.add(self.zonePourNombres)
>
>
>     def toucheEnfoncee(self, widget, event):
>       print "toucheEnfoncee : "+str(event.keyval)
>       if event.keyval >= ord('1') and event.keyval <= ord('9'):
>         self.zonePourNombres.set_text(chr(event.keyval))
>         self.zonePourNombres.set_position(1)
>         self.zonePourNombres.select_region(0,0)
>         self.interfacePourNombres.show_all()
>
>
>     def obtenirLeNombre(self, widget):
>       self.interfacePourNombres.hide()
>       print "nombre obtenu : "+str(widget.get_text())
>
>     def expose(self, widget, event):
>       pass
>
> PyApp()
> gtk.main()
>
>
> _______________________________________________
> pygtk mailing list   pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://faq.pygtk.org/



More information about the pygtk mailing list