This example is similar to , except we use Glade to create the toolbar in an XML .ui file.
To create the toolbar using the Glade Interface Designer:
Open Glade, and save the file as
Under
Under the
Under the
Right click on the toolbar in the top right and select
We want to add 5 ToolButtons: New, Open, Undo, Fullscreen and Leave Fullscreen. First, we will add the New ToolButton.
Under
Change the name of the ToolItem to new_button.
Scroll down and set
Enter the
Change the
Select the
Repeat the above steps for the remaining ToolButtons, with the following properties:
Name |
Is important |
Action name |
Label |
Stock Id |
open_button |
Yes |
app.open |
Open |
gtk-open |
undo_button |
Yes |
win.undo |
Undo |
gtk-undo |
fullscreen_button |
Yes |
win.fullscreen |
Fullscreen |
gtk-fullscreen |
leave_fullscreen_button |
Yes |
win.fullscreen |
Leave Fullscreen |
gtk-leave-fullscreen |
Close the
When our program will first start, we do not want the show_all()
would override this setting - so in the code we have to use show()
separately on all the elements.
Save your work, and close Glade.
The XML file created by Glade is shown below. This is the description of the toolbar. At the time of this writing, the option to add the class Gtk.STYLE_CLASS_PRIMARY_TOOLBAR in the Glade Interface did not exist. We can manually add this to the XML file. To do this, add the following XML code at line 9 of
]]>
If you do not add this, the program will still work fine. The resulting toolbar will however look slightly different then the screenshot at the top of this page.
We now create the code below, which adds the toolbar from the file we just created.
For the useful methods for a Toolbar widget, see
Gtk.Builder builds an interface from an XML UI definition.
add_from_file(filename)
loads and parses the given file and merges it with the current contents of the Gtk.Builder.
add_from_string(string)
parses the given string and merges it with the current contents of the Gtk.Builder.
add_objects_from_file(filename, object_ids)
is the same as add_from_file()
, but it loads only the objects with the ids given in the object_id
s list.
add_objects_from_string(string, object_ids)
is the same as add_from_string()
, but it loads only the objects with the ids given in the object_id
s list.
get_object(object_id)
retrieves the widget with the id object_id
from the loaded objects in the builder.
get_objects()
returns all loaded objects.
connect_signals(handler_object)
connects the signals to the methods given in the handler_object
. This can be any object which contains keys or attributes that are called like the signal handler names given in the interface description, e.g. a class or a dict. In line 39 the signal "activate"
from the action undo_action
is connected to the callback function undo_callback()
using action.connect(signal, callback function)
. See for a more detailed explanation.
In this sample we used the following:
GtkGrid
GtkBuilder
GtkWidget
Event Structures