【问题标题】:How to get a gtkDialog's default response to trigger of the space bar as well如何获得 gtkDialog 对空格键触发的默认响应
【发布时间】:2010-10-22 19:05:52
【问题描述】:

我设置了一个 messageDialog,因此它的默认响应是 gtk.RESPONSE_OK,因此即使确定按钮没有焦点,当用户按 Enter 键时也会单击确定按钮。我还想让空格键触发 default_response。最好的方法是什么?

这是在 linux 环境中使用 python 2.4 的。不幸的是我没有升级python的权限。

【问题讨论】:

    标签: python dialog gtk key key-bindings


    【解决方案1】:

    连接到消息对话框上的key-press-event 信号:

    def on_dialog_key_press(dialog, event):
        if event.string == ' ':
            dialog.response(gtk.RESPONSE_OK)
            return True
        return False
    
    dialog = gtk.MessageDialog(message_format='Some message', buttons=gtk.BUTTONS_OK_CANCEL)
    dialog.add_events(gtk.gdk.KEY_PRESS_MASK)
    dialog.connect('key-press-event', on_dialog_key_press)
    dialog.run()
    

    但请记住,改变用户对用户界面的期望通常被认为不酷。

    【讨论】:

    • 我尝试这样做的唯一原因是它是特别要求的。
    【解决方案2】:

    我是 pygtk 的菜鸟,但我无法让 @ptomato 的示例 +“hello world”样板工作,除非我响应空间并返回并添加对 dialog.destroy() 的调用。物有所值。

    #!/usr/bin/env python
    
    # example helloworld.py
    
    import pygtk
    pygtk.require('2.0')
    import gtk
    
    def md_event(dialog, event):
        if event.keyval in (gtk.keysyms.Return, gtk.keysyms.space):
            dialog.response(gtk.RESPONSE_OK)
            dialog.destroy()
            return True
        elif event.keyval == gtk.keysyms.Escape:
            dialog.response(gtk.RESPONSE_CANCEL)
            dialog.destroy()
            return True
        return False
    
    class HelloWorld:
        # This is a callback function. The data arguments are ignored
        # in this example. More on callbacks below.
        def hello(self, widget, data=None):
            print "Hello World"
    
        # Another callback
        def destroy(self, widget, data=None):
            gtk.main_quit()
    
        def create_message_dialog(self, x, y):
            md = gtk.MessageDialog(buttons=gtk.BUTTONS_OK_CANCEL, message_format="wawawawaaaaa")
            md.add_events(gtk.gdk.KEY_PRESS_MASK)
            md.connect("key-press-event", md_event)
            result = md.run()
            print result
    
        def __init__(self):
            # create a new window
            self.window = gtk.Window(gtk.WINDOW_TOPLEVEL)
    
            # Here we connect the "destroy" event to a signal handler.
            # This event occurs when we call gtk_widget_destroy() on the window,
            # or if we return FALSE in the "delete_event" callback.
            self.window.connect("destroy", self.destroy)
    
            # Sets the border width of the window.
            self.window.set_border_width(10)
    
            self.button2 = gtk.Button("Message Dialog")
            self.button2.connect("clicked", self.create_message_dialog, None)
            self.window.add(self.button2)
            self.button2.show()
    
            # and the window
            self.window.show()
    
        def main(self):
            # All PyGTK applications must have a gtk.main(). Control ends here
            # and waits for an event to occur (like a key press or mouse event).
            gtk.main()
    
    def run_hello():
        hello = HelloWorld()
        hello.main()
    
    # If the program is run directly or passed as an argument to the python
    # interpreter then create a HelloWorld instance and show it
    if __name__ == "__main__":
        run_hello()
    

    【讨论】:

    • 奇怪...除了import gtk,我没有编辑任何内容!
    猜你喜欢
    • 2016-11-05
    • 1970-01-01
    • 2016-09-14
    • 1970-01-01
    • 2014-02-13
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    • 1970-01-01
    相关资源
    最近更新 更多