From ae69a50576da916dc810f781a44792b6968e32bd Mon Sep 17 00:00:00 2001 From: Johan Dahlin Date: Tue, 30 Mar 2004 09:48:47 +0000 Subject: Updated, call dialog.destroy() * examples/gtk/filechooser.py (response): Updated, call dialog.destroy() * examples/gtk/uimanager.py: New example * gtk/gtk.override (_wrap_gtk_ui_manager_add_ui_from_string): Override, get rid of second argument. --- examples/gtk/filechooser.py | 7 ++- examples/gtk/uimanager.py | 126 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 examples/gtk/uimanager.py (limited to 'examples/gtk') diff --git a/examples/gtk/filechooser.py b/examples/gtk/filechooser.py index 470b4917..b50be0cc 100644 --- a/examples/gtk/filechooser.py +++ b/examples/gtk/filechooser.py @@ -22,5 +22,10 @@ filter.add_mime_type("image/jpeg") filter.add_mime_type("image/gif") dialog.add_filter(filter) -if dialog.run() == gtk.RESPONSE_OK: +response = dialog.run() +dialog.destroy() +if response == gtk.RESPONSE_OK: print dialog.get_filename(), 'selected' +elif response == gtk.RESPONSE: + print 'Closed, no files selected' + diff --git a/examples/gtk/uimanager.py b/examples/gtk/uimanager.py new file mode 100644 index 00000000..6fa1281e --- /dev/null +++ b/examples/gtk/uimanager.py @@ -0,0 +1,126 @@ +# +# Small example of the new GtkUIManager +# +# Johan Dahlin , 2004 +# + +import pygtk +pygtk.require('2.0') + +import gtk + +if gtk.pygtk_version > (2,3,90): + print "PyGtk 2.3.90 or later required for this example" + raise SystemExit + +window_actions = [ + ('FileMenu', None, '_File'), + ('New', gtk.STOCK_NEW, '_New', 'N', 'Create a new file', 'file_new_cb'), + ('Open', gtk.STOCK_OPEN, '_Open', 'O', 'Open a file', 'file_open_cb'), + ('Quit', gtk.STOCK_QUIT, '_Quit', 'Q', 'Quit application', 'file_quit_cb'), + ('HelpMenu', None, '_Help'), + ('About', None, '_About', None, 'About application', 'help_about_cb'), + ] + +ui_string = """ + + + + + + + + + + + +""" + +def fix_actions(actions, instance): + "Helper function to map methods to an instance" + retval = [] + + for i in range(len(actions)): + curr = actions[i] + if len(curr) > 5: + curr = list(curr) + curr[5] = getattr(instance, curr[5]) + curr = tuple(curr) + + retval.append(curr) + return retval + +class Window(gtk.Window): + def __init__(self): + gtk.Window.__init__(self) + self.connect('delete-event', self.delete_event_cb) + self.set_size_request(400, 200) + vbox = gtk.VBox() + self.add(vbox) + + menu = self.create_ui() + vbox.pack_start(menu, expand=False) + + sw = gtk.ScrolledWindow() + vbox.pack_start(sw) + + textview = gtk.TextView() + self.buffer = textview.get_buffer() + sw.add(textview) + + status = gtk.Statusbar() + vbox.pack_end(status, expand=False) + + def create_ui(self): + ag = gtk.ActionGroup('WindowActions') + + actions = fix_actions(window_actions, self) + ag.add_actions(actions) + self.ui = gtk.UIManager() + self.ui.insert_action_group(ag, 0) + self.ui.add_ui_from_string(ui_string) + menu = self.ui.get_widget('/MenuBar') + return menu + + def file_new_cb(self, action): + self.buffer.set_text("") + + def file_open_cb(self, action): + dialog = gtk.FileChooserDialog("Open..", + None, + gtk.FILE_CHOOSER_ACTION_OPEN, + (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, + gtk.STOCK_OPEN, gtk.RESPONSE_OK)) + dialog.set_default_response(gtk.RESPONSE_OK) + + filter = gtk.FileFilter() + filter.set_name("All files") + filter.add_pattern("*") + dialog.add_filter(filter) + + if dialog.run() == gtk.RESPONSE_OK: + dialog.hide() + filename = dialog.get_filename() + self.buffer.set_text(file(filename).read()) + + def file_quit_cb(self, action): + self.close() + + def help_about_cb(self, action): + dialog = gtk.MessageDialog(self, + gtk.DIALOG_MODAL | gtk.DIALOG_DESTROY_WITH_PARENT, + gtk.MESSAGE_INFO, gtk.BUTTONS_OK, + "Small example of the new GtkUIManger class") + dialog.run() + dialog.destroy() + + def delete_event_cb(self, window, event): + self.close() + + def close(self): + gtk.main_quit() + +if __name__ == '__main__': + w = Window() + w.show_all() + gtk.main() -- cgit v1.2.1