#!/usr/bin/env python # example toolbar.py import gtk import gtkxpm class ToolbarExample: # This method is connected to the Close button or # closing the window from the WM def delete_event(self, widget, event=None): gtk.mainquit() return gtk.FALSE # that's easy... when one of the buttons is toggled, we just # check which one is active and set the style of the toolbar # accordingly # But because we created and passed the radio buttons and toggle button # as widgets we have to explicitly hide() and show() the icons and # labels. Wouldn't have to do this if the GtkToolbar.*_element methods # were available in PyGTK def radio_event(self, widget, data): widgets = (self.text_button, self.icon_button, self.both_button, self.tooltips_button) if self.text_button.active: self.toolbar.set_style(gtk.TOOLBAR_TEXT) for w in widgets: w.children()[0].children()[0].hide() w.children()[0].children()[1].show() elif self.icon_button.active: self.toolbar.set_style(gtk.TOOLBAR_ICONS) for w in widgets: w.children()[0].children()[0].show() w.children()[0].children()[1].hide() elif self.both_button.active: self.toolbar.set_style(gtk.TOOLBAR_BOTH) for w in widgets: w.children()[0].children()[0].show() w.children()[0].children()[1].show() # even easier, just check given toggle button and enable/disable # tooltips def toggle_event(self, widget, data): self.toolbar.set_tooltips(widget.active) # helper method to create a radiobutton that can be put in a toolbar # as a widget def create_radiobutton(self, toolbar, prevbutton, labeltext, icon, cb, data): radiobutton = gtk.GtkRadioButton(prevbutton) radiobutton.set_relief(toolbar.get_button_relief()) radiobutton.set_mode(gtk.FALSE) radiobutton.unset_flags(gtk.CAN_FOCUS) if cb: radiobutton.connect("clicked", cb, data) vbox = gtk.GtkVBox(gtk.FALSE, 0) radiobutton.add(vbox) vbox.show() if labeltext: label = gtk.GtkLabel(labeltext) vbox.pack_end(label) # we have no way to find out what the toolbar style is so we just # assume that the user set things right label.show() if icon: vbox.pack_end(icon) icon.show() radiobutton.show() return radiobutton def __init__(self): # Here is our main window (a dialog) and a handle for the handlebox # Ok, we need a toolbar, an icon with a mask (one for all of # the buttons) and an icon widget to put this icon in (but # we'll create a separate widget for each button) # create a new window with a given title, and nice size self.dialog = gtk.GtkDialog() self.dialog.set_title("GTKToolbar Tutorial") self.dialog.set_usize(600, 300) self.dialog.allow_shrink = gtk.TRUE # typically we quit if someone tries to close us self.dialog.connect("delete_event", self.delete_event) # we need to realize the window because we use pixmaps for # items on the toolbar in the context of it self.dialog.realize() # to make it nice we'll put the toolbar into the handle box, # so that it can be detached from the main window handlebox = gtk.GtkHandleBox() self.dialog.vbox.pack_start(handlebox, gtk.FALSE, gtk.FALSE, 5) # toolbar will be horizontal, with both icons and text, and # with 5pxl spaces between items and finally, # we'll also put it into our handlebox toolbar = gtk.GtkToolbar(gtk.ORIENTATION_HORIZONTAL, gtk.TOOLBAR_BOTH) toolbar.set_border_width(5) toolbar.set_space_size(5) handlebox.add(toolbar) self.toolbar = toolbar # now we create icon with mask: we'll reuse it to create # icon widgets for toolbar items icon, mask = gtk.create_pixmap_from_xpm_d( self.dialog.get_window(), self.dialog.get_style().white, gtkxpm.gtk_xpm) # our first item is button iconw = gtk.GtkPixmap(icon, mask) # icon widget close_button = toolbar.append_item( "Close", # button label "Closes this app", # this button's tooltip "Private", # tooltip private info iconw, # icon widget self.delete_event) # a signal toolbar.append_space() # space after item # now, let's make our radio buttons group... using the # create_radiobutton helper method iconw = gtk.GtkPixmap(icon, mask) icon_button = self.create_radiobutton( toolbar, None, # button in same radiobutton group "Icon", # label iconw, # icon self.radio_event, # signal toolbar) # data for signal # add the button to the toolbar toolbar.append_widget( icon_button, # widget "Only icons in toolbar", # tooltip "Private") # tooltip private string toolbar.append_space() self.icon_button = icon_button # following radio buttons refer to previous ones iconw = gtk.GtkPixmap(icon, mask) text_button = self.create_radiobutton( toolbar, icon_button, "Text", iconw, self.radio_event, toolbar) toolbar.append_widget( text_button, "Only texts in toolbar", "Private") toolbar.append_space() self.text_button = text_button iconw = gtk.GtkPixmap(icon, mask) both_button = self.create_radiobutton( toolbar, text_button, "Both", iconw, self.radio_event, toolbar) toolbar.append_widget( both_button, "Icons and text in toolbar", "Private") toolbar.append_space() self.both_button = both_button # here we have just a simple toggle button iconw = gtk.GtkPixmap(icon, mask) tooltips_button = gtk.GtkToggleButton() self.tooltips_button = tooltips_button tooltips_button.connect("toggled", self.toggle_event, toolbar) vbox = gtk.GtkVBox(gtk.FALSE, 0) tooltips_button.add(vbox) vbox.pack_end(gtk.GtkLabel("Tooltips")) vbox.pack_end(iconw) vbox.show_all() toolbar.append_widget( tooltips_button, "Toolbar with or without tips", "Private") tooltips_button.show() toolbar.append_space() tooltips_button.set_active(gtk.TRUE) both_button.set_active(gtk.TRUE) # to pack a widget into toolbar, we only have to # create it and append it with an appropriate tooltip entry = gtk.GtkEntry() toolbar.append_widget(entry, "This is just an entry", "Private") # well, it isn't created within the toolbar, so we must still show it entry.show() # that's it ! let's show everything. toolbar.show() handlebox.show() self.dialog.show() def main(): # rest in gtk_main and wait for the fun to begin! gtk.mainloop() return 0 if __name__ == "__main__": ToolbarExample() main()