Gtk.Dialog 在 Python 中按键正常

Gtk.Dialog keypress as OK in Python

我有一个登录对话框,它工作正常,但作为 response_ok 它只接受确定按钮(尽管作为取消我可以从键盘按 esc)。我试着让它对 ENTER 键做出反应,我认为应该很容易,但对我来说不是。我尝试使用 set_default_response 和其他一些但它不起作用。我希望有比连接信号更简单的方法。这是代码:

def get_user_pw(self):
    dialogWindow = Gtk.MessageDialog(self,Gtk.DialogFlags.MODAL|Gtk.DialogFlags.DESTROY_WITH_PARENT,Gtk.MessageType.QUESTION,Gtk.ButtonsType.OK_CANCEL,"Log in")
    dialogBox = dialogWindow.get_content_area()
    login = Gtk.Entry()
    pas = Gtk.Entry()
    pas.set_visibility(False)
    pas.set_invisible_char("*")
    dialogBox.pack_end(has, False, False, 0)
    dialogBox.pack_end(uzytkownik, False, False, 0)
    dialogWindow.show_all()
    Gtk.Dialog.set_default_response(dialogWindow,response_id=Gtk.ResponseType.OK)
    response = dialogWindow.run()
    {some action here}
    dialogWindow.destroy()

几天前我遇到了同样的问题,结果证明解决方案是在 Entry.

activate 信号上使用监听器

所以我编辑了你的代码,在用户名字段中按 Enter 将焦点移动到密码字段,在密码字段中按 Enter 模拟单击 确定

def get_user_pw(self):
    # Create the dialog window
    self.dialogWindow = Gtk.MessageDialog(self,Gtk.DialogFlags.MODAL|Gtk.DialogFlags.DESTROY_WITH_PARENT,Gtk.MessageType.QUESTION,Gtk.ButtonsType.OK_CANCEL,"Log in")

    # Get the content area
    dialogBox = self.dialogWindow.get_content_area()

    # Create fields
    login = Gtk.Entry()
    pas = Gtk.Entry()
    pas.set_visibility(False)
    pas.set_invisible_char("*")

    # Add the fields to the dialog
    dialogBox.pack_end(pas, False, False, 0)
    dialogBox.pack_end(login, False, False, 0)

    # Connect activate to the username field to move to the password field when enter is hit
    login.__next_field = pas
    login.connect("activate", self.next_field)

    # Connect activate to submit the data when enter is hit
    pas.connect("activate", self.submit)

    self.dialogWindow.show_all()

    response = self.dialogWindow.run()
    self.dialogWindow.destroy()

def submit(self, entry):
    # Send the OK response to the dialog
    self.dialogWindow.response(Gtk.ResponseType.OK)

def next_field(self, entry):
    # Move the focus to the next field
    if entry.__next_field is not None:
        entry.__next_field.grab_focus()