Python中GTK3对话框,Gtk.Entry的"回车键"应触发OK按钮

7 投票
2 回答
2525 浏览
提问于 2025-04-18 08:13

我有以下用Python和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

现在我想让用户在“taskDescription”输入框里按下回车键时,自动点击确认按钮。有没有什么办法可以做到这一点?非常感谢!

编辑:

我找到了解决办法……虽然不确定这是否是正确的方法,但这个方法有效:

我在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()

2 个回答

0

这是另一个关于Gtk3对话框的例子,它会询问一些SpinButton的值:

class Days_Back:

    def __init__(self):
        self.days = self.days_back()

    def days_back(self, title="Days Back", max_number=10):
        __title__ = title

        flags = Gtk.DialogFlags.MODAL
        dialog = Gtk.Dialog(title=__title__,
                           flags=flags,
                           buttons=(Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL,
                           Gtk.STOCK_OK, Gtk.ResponseType.OK))

        dialog.set_default_response(Gtk.ResponseType.OK)

        box = dialog.get_content_area()
        adjustment = Gtk.Adjustment(1, 1, max_number, 1, 5, 0)
        spin_button = Gtk.SpinButton(adjustment=adjustment, climb_rate=1, digits=0)
        spin_button.show()
        spin_button.set_activates_default(True)
        box.add(spin_button)

        response = dialog.run()
        dialog.destroy()

        if response == Gtk.ResponseType.OK:
            logging.debug("you choosed Gtk.ResponseType.OK")
            return spin_button.get_value_as_int()
        else:
            exit(response)
4

我找到了解决办法……虽然不确定这是不是正确的做法,但这个方法有效:

我在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()

撰写回答