GTK3 Dialog in Python, "enter key" on a Gtk.Entry should trigger the OK Button
gtk, gtk3, python
Solution
I found a solution... Not sure if it is the way it should be done, but this works:
I added following lines to the init method of DialogTaskDescription():
# enter key should trigger the default action
self.taskDescription.set_activates_default(True)
# make OK button the default
okButton = self.get_widget_for_response(response_id=Gtk.ResponseType.OK)
okButton.set_can_default(True)
okButton.grab_default()
Problem
I have following code written in Python with Gtk3. ``` from gi.repository import Gtk class DialogTaskDescription(Gtk.Dialog): def __init__(self): Gtk.Dialog.__init__(self, "Create ToDo.txt Entry", 0, 0, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, Gtk.STOCK_OK, Gtk.ResponseType.OK)) self.set_default_size(150, 100) hbox = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=6) self.label = Gtk.Label("Task Description:") self.taskDescription = Gtk.Entry() hbox.pack_start(self.label, False, True, 0) hbox.pack_start(self.taskDescription, True, True, 0) box = self.get_content_area() box.add(hbox) self.show_all() def getTaskDescription(self): return self.taskDescription.get_text() class TaskDescription: def __init__(self): self.dialog = DialogTaskDescription() def getTaskDescription(self): response = self.dialog.run() taskDescription = '' if response == Gtk.ResponseType.OK: taskDescription = self.dialog.getTaskDescription() self.dialog.destroy() return taskDescription ``` Now I would like to trigger a click on the OK-button if the user press the enter key on the "taskDescription" entry field. Any idea how I could do this? Thanks a lot! EDIT: I found a solution... Not sure if it is the way it should be done, but this works: I added following lines to the init method of DialogTaskDescription(): ``` # enter key should trigger the default action self.taskDescription.set_activates_default(True) # make OK button the default okButton = self.get_widget_for_response(response_id=Gtk.ResponseType.OK) okButton.set_can_default(True) okButton.grab_default() ```