Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set default button in PyGTK?

I have very simple window where I have 2 buttons - one for cancel, one for apply. How to set the button for apply as default one? (When I press enter, "apply" button is pressed)

However, I want to set focus to the first input widget (I can't use grab_focus() on the button)

Any suggestions?

Edit: After wuub's answer it works visually good. However, when I press the button in different widget, it doesn't run callback of the default button.

Example code:

import os, sys, pygtk, gtk 

def run(button, window):
    dialog = gtk.MessageDialog(window, gtk.DIALOG_MODAL, gtk.MESSAGE_INFO, gtk.BUTTONS_OK, "OK")
    dialog.run()
    dialog.destroy()

window = gtk.Window()
window.connect("destroy", gtk.main_quit)

vbox = gtk.VBox(spacing = 10)
entry = gtk.Entry()
vbox.pack_start(entry)

button = gtk.Button(stock = gtk.STOCK_SAVE)
button.connect("clicked", run, window)
button.set_flags(gtk.CAN_DEFAULT)
window.set_default(button)
vbox.pack_start(button)

window.add(vbox)
window.show_all()
gtk.main()

EDIT2: Every input which can activate default widget must be ran

widget.set_activates_default(True)
like image 401
izidor Avatar asked Jul 11 '09 20:07

izidor


1 Answers

http://www.pygtk.org/docs/pygtk/class-gtkdialog.html#method-gtkdialog--set-default-response

http://www.pygtk.org/docs/pygtk/class-gtkwindow.html#method-gtkwindow--set-default

like image 190
Wojciech Bederski Avatar answered Oct 02 '22 04:10

Wojciech Bederski