Dan
Dan

Reputation: 3725

Custom python widgets in glade 3

I would like to subclass an existing Gtk widget and get it to work with Glade3. The code is all python, and I'm using PyGObject.

What I want to do is extend Gtk.TextView and be able to add it to my glade UI as if it were a native widget.

I read that it was possible with Glade 2, but I'm not sure how to do this with Glade 3, and I can't seem to find any examples or documentation on this. Any help would be appreciated. Thanks.

Upvotes: 4

Views: 1627

Answers (1)

Dan
Dan

Reputation: 3725

Finally found out how to do it. First, you have to declare that your glade file depends upon a catalog.

<?xml version="1.0" encoding="UTF-8"?>
<interface>
  <!-- interface-requires gtk+ 3.0 -->
  <!-- interface-requires the_catalog_name 1.0 -->
  ...Rest of the glade file...

Next, create a catalog.xml file. To extend a treeview, it should look something like this:

<glade-catalog name="the_catalog_name" domain="glade-3" 
               depends="gtk+" version="1.0">
  <glade-widget-classes>
    <glade-widget-class title="My Custom Text View" name="CustomTextView" 
                        generic-name="CustomTextView" parent="GtkTextView"
                        icon-name="widget-gtk-textview"/>
  </glade-widget-classes>
</glade-catalog>

And that's it. Obviously There's no python code written yet, but that won't prevent Glade from displaying your custom widget. It will just treat it as a normal TextView, Label, or whatever else you want to extend.

Oh, and I couldn't find a way in Glade itself to create a CustomTextView unfortunately. But what you can do is add a regular TextView in glade, save the glade file and open it in a text editor. Just look for the treeview you created and change like so:

-              <object class="TreeView" id="treeview1">
+              <object class="CustomTreeView" id="treeview1">

Open it back up in glade and it shows up. Then just define a CustomTreeView class in python and extend as needed.

Upvotes: 6

Related Questions