如何获得 gtkDialog 对空格键触发的默认响应

发布于 2024-09-28 09:04:36 字数 181 浏览 7 评论 0原文

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

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

I have a messageDialog set up so that its default response is gtk.RESPONSE_OK so the okay button is clicked when the user hits enter even if the okay button does not have focus. I would like to also have the space bar trigget the default_response. What is the best way to do this?

This is with python 2.4 in a linux environment. Unfortunately I don't have permission to upgrade python.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

呆头 2024-10-05 09:04:36

连接到消息对话框上的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()

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

Connect to the key-press-event signal on the message dialog:

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

Bear in mind, though, that changing users' expectations of the user interface is generally considered Not Cool.

乞讨 2024-10-05 09:04:36

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

I'm a total noob at pygtk, but I could not get @ptomato's example + "hello world" boilerplate to work unless I responded to space and return plus added a call to dialog.destroy(). Take it for what it is worth.

#!/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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文