Gtk.Dialog按键在Python中可以正常使用

时间:2016-02-04 11:30:55

标签: python dialog keypress gtk3

我有一个登录对话框,它工作正常,但作为response_ok它只接受OK按钮(虽然我可以从键盘上按CANCEL取消)。我尝试让它对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()

1 个答案:

答案 0 :(得分:2)

几天前我确实遇到了同样的问题,事实证明解决方案是在Entry的{​​{1}}信号上使用听众。

所以我编辑了你的代码,以便在用户名字段中按 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()