How to set default button in PyGTK?

2024/9/22 20:24:38

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)
Answer

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

https://en.xdnf.cn/q/71906.html

Related Q&A

Can Python recognize changes to a file that it is running interactively?

I was doing some troubleshooting and I was curious if it is possible to run a Python script interactively, change a function defined in the script, save the file, then have the interactive shell recogn…

How to use FTP with Pythons requests

Is it possible to use the requests module to interact with a FTP site? requests is very convenient for getting HTTP pages, but I seem to get a Schema error when I attempt to use FTP sites. Is there …

How to find a best fit distribution function for a list of data?

I am aware of many probabilistic functions builted-in Python, with the random module. Id like to know if, given a list of floats, it would be possible to find the distribution equation that best fits t…

How to use peewee limit()?

With Peewee Im trying to use limit as follows:one_ticket = Ticket.select().limit(1) print one_ticket.count()This prints out 5 however. Does anybody know whats wrong here?

Python (1..n) syntax?

I see in the code on this Sage wiki page the following code:@interact def _(order=(1..12)):Is this (1..n) syntax unique to Sage or is it something in Python? Also, what does it do?

in python , How to load just one time a ML model in a rest service like django or flask?

I have a ML model saved in a pkl (pickel file), I have no problem loading this model and using it for prediction, even I have a rest service that expose it, the only problem is that I load the model i…

Adding a Title or Text to a Folium Map

Im wondering if theres a way to add a title or text on a folium map in python?I have 8 maps to show and I want the user to know which map theyre looking at without having to click on a marker. I attem…

Zombie SharedDataMiddleware on Python Heroku

Im setting up a Flask app on Heroku. Everything is working fine until I added static files. Im using this:from werkzeug import SharedDataMiddleware app = Flask(__name__) app.wsgi_app = SharedDataMiddle…

Python: Urllib.urlopen nonnumeric port

for the following codetheurl = "https://%s:%[email protected]/nic/update?hostname=%s&myip=%s&wildcard=NOCHG&mx=NOCHG&backmx=NOCHG" % (username, password, hostname, theip)conn…

How to unittest the sequence of function calls made inside a python fuction?

I would like to unittest a fuction and assert if the sequence of function calls made inside the function workflow(). Something like,[1st called] fetch_yeargroup_ls()[2nd called] invoke_get_links().....…