Search code examples
pythongtk2

How to set an GTK Entry as an default active element in Python GTK2


How can I set a GTK entry as default activated element / widget on start so I can insert my text without click into the entry widget first?

For example, when I start this script I want to insert into entry2 by default:

#!/usr/bin/python
# -*- coding: utf-8 -*-

import gtk

window = gtk.Window(gtk.WINDOW_TOPLEVEL)
mainbox    = gtk.VBox()
window.add(mainbox)
mainbox.pack_start(gtk.Label("Label 1"))
entry1 = gtk.Entry()
mainbox.pack_start(entry1)
mainbox.pack_start(gtk.Label("Label 2"))
entry2 = gtk.Entry()
mainbox.pack_start(entry2)
window.show_all()
gtk.main()

I could not find an option:


Solution

  • Just found:

    5.10. How do I focus a widget? How do I focus it before its toplevel window is shown?

    http://faq.pygtk.org/index.py?file=faq05.010.htp&req=show

    entry2.grab_focus()
    

    .

    #!/usr/bin/python
    # -*- coding: utf-8 -*-
    
    import gtk
    
    window  = gtk.Window(gtk.WINDOW_TOPLEVEL)
    mainbox = gtk.VBox()
    window.add(mainbox)
    mainbox.pack_start(gtk.Label("Label 1"))
    entry1 = gtk.Entry()
    mainbox.pack_start(entry1)
    mainbox.pack_start(gtk.Label("Label 2"))
    entry2 = gtk.Entry()
    mainbox.pack_start(entry2)
    
    entry2.grab_focus()
    
    window.show_all()
    
    gtk.main()