PyGTK:访问gtk.MessageDialog中的按钮?

时间:2011-11-26 19:04:15

标签: python pygtk gtk2

我有一个在PyGTK中使用gtk.MessageDialog创建提示的函数。我怎样才能访问预定义的按钮?或者我需要手动构建一个gtk.Dialog?我不愿意,因为MessageDialog是一个便利功能。

功能:

def gtkPrompt(self, name):
    # Create new GTK dialog with all the fixings
    prompt = gtk.MessageDialog(None, 0, gtk.MESSAGE_QUESTION, gtk.BUTTONS_OK_CANCEL, name)
    # Set title of dialog
    prompt.set_title("Prompt")
    # Create and add entry box to dialog
    entry = gtk.Entry()
    prompt.vbox.add(entry)
    # Show all widgets in prompt
    prompt.show_all()
    # Run dialog until user clicks OK or Cancel
    if prompt.run() == gtk.RESPONSE_CANCEL:
        # User cancelled dialog
        rval = False
    else:
        # User clicked OK, grab text from entry box
        rval = entry.get_text()
    # Destory prompt
    prompt.destroy()
    # Give the good (or bad) news
    return rval

3 个答案:

答案 0 :(得分:4)

您可以使用get_children()转到“确定”按钮:

def yesNoDialog(window, message, default=False):
    dialog=gtk.MessageDialog(window, gtk.DIALOG_MODAL |
                             gtk.DIALOG_DESTROY_WITH_PARENT,
                             gtk.MESSAGE_QUESTION,
                             gtk.BUTTONS_YES_NO, message)
    if default:
        h_button_box=dialog.vbox.get_children()[1]
        yes_button=h_button_box.get_children()[0]
        yes_button.grab_default()
    response=dialog.run()
    dialog.destroy()
    if response==gtk.RESPONSE_YES:
        return True
    else:
        return False

答案 1 :(得分:2)

gtk.MessageDialoggtk.Dialog的子类。 gtk.Dialog个对象将其按钮存储在gtk.HBox属性下的action_area

在代码中:

> prompt.action_area.get_children()
[<gtk.Button object at 0x18c0aa0 (GtkButton at 0x130e990)>, <gtk.Button object at 0x18c0af0 (GtkButton at 0x130e8d0)>]

答案 2 :(得分:2)

从2.22开始,您可以使用get_widget_for_response()方法。例如:

cancelButton = dialog.get_widget_for_response(response_id=gtk.RESPONSE_CANCEL)