gtkBuild2Py

gtkbuild2py

gtkbuild2py

PROJECT DISCONTINUED. No files to download.

gtkBuild2Py is a small utility that auto-generates python code from an .xml file created with GtkBuilder.
A small faq about GtkBuilder and glade can be found at Micah Carrick’s excelent blog here. See bottom of this page for a short description about GtkBuilder.

gtkBuild2Py just parses that .xml file and generates python code that it’s ready for you to use, with all your widgets and the signals inserted.
Scroll down to the bottom for a lengthy example of an .xml and an auto-generated python code.

The usage of gtkBuild2Py is simple.

  • 1. Choose the .xml file that contains your gui.
  • 2. (Optional) Put the name you want for your main application Class. If you leave this field blank, then your Class will be named ‘pyGtkApp’ by default.
  • 3. (Optional) Put the name you want for the generated .py file. If you leave this field blank, then the .py file will be named after the .xml file.

The usage is pretty straightforward, you’ll see it from the moment you’ll run the program. The generated .py file will be created in the same folder where the .xml file is.

Requirements: Python 2.5.2, PyGTK 2.12.1

Just unzip it where you want to, and start gtkBuild2Py.py either from the command line or by double-clicking it (assuming you gave it permission to be executable.)

For bugs, feedback, recomendations or whatever, send either an e-mail or write a comment here.
gtkBuild2Py is released under the GNU GPL license, that means it is free software.

About GtkBuilder
GtkBuilder just parses a .glade file created with glade3 and creates an .xml file to use with your code. GtkBuilder is an integral part of gtk+ and it will replace libglade in the future.
To create an .xml file from a .glade one, just cd to the directory where your .glade file is and type:
gtk-builder-convert my_file.glade my_file.xml (change the names accordingly)

Example code of gtkBuild2Py
From the following lengthy editor.xml file (it’s a small text editor in the beginning):


<?xml version="1.0"?>
<!--Generated with glade3 3.4.5 on Wed Sep 24 21:15:31 2008 -->
<interface>
  <object class="GtkUIManager" id="uimanager1">
    <child>
      <object class="GtkActionGroup" id="actiongroup1">
        <child>
          <object class="GtkAction" id="mnuFile">
<property name="name">mnuFile</property>
<property name="label" translatable="yes">_File</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuNew">
<property name="stock_id">gtk-new</property>
<property name="name">mnuNew</property>
<property name="label" translatable="yes">_New</property>
            <signal handler="on_mnuNew_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuOpen">
<property name="stock_id">gtk-open</property>
<property name="name">mnuOpen</property>
<property name="label" translatable="yes">_Open</property>
            <signal handler="on_mnuOpen_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuSave">
<property name="stock_id">gtk-save</property>
<property name="name">mnuSave</property>
<property name="label" translatable="yes">_Save</property>
            <signal handler="on_mnuSave_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuSaveAs">
<property name="stock_id">gtk-save-as</property>
<property name="name">mnuSaveAs</property>
<property name="label" translatable="yes">Save _As</property>
            <signal handler="on_mnuSaveAs_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuQuit">
<property name="stock_id">gtk-quit</property>
<property name="name">mnuQuit</property>
<property name="label" translatable="yes">_Quit</property>
            <signal handler="on_mnuQuit_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuEdit">
<property name="name">mnuEdit</property>
<property name="label" translatable="yes">_Edit</property>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuUndo">
<property name="stock_id">gtk-undo</property>
<property name="name">mnuUndo</property>
<property name="label" translatable="yes">_Undo</property>
            <signal handler="on_mnuUndo_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuRedo">
<property name="stock_id">gtk-redo</property>
<property name="name">mnuRedo</property>
<property name="label" translatable="yes">_Redo</property>
            <signal handler="on_mnuRedo_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuCut">
<property name="stock_id">gtk-cut</property>
<property name="name">mnuCut</property>
<property name="label" translatable="yes">Cu_t</property>
            <signal handler="on_mnuCut_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuCopy">
<property name="stock_id">gtk-copy</property>
<property name="name">mnuCopy</property>
<property name="label" translatable="yes">_Copy</property>
            <signal handler="on_mnuCopy_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuPaste">
<property name="stock_id">gtk-paste</property>
<property name="name">mnuPaste</property>
<property name="label" translatable="yes">_Paste</property>
            <signal handler="on_mnuPaste_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuDelete">
<property name="stock_id">gtk-delete</property>
<property name="name">mnuDelete</property>
<property name="label" translatable="yes">_Delete</property>
            <signal handler="on_mnuDelete_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuSelectAll">
<property name="stock_id">gtk-select-all</property>
<property name="name">mnuSelectAll</property>
<property name="label" translatable="yes">Select All</property>
            <signal handler="on_mnuSelectAll_activate" name="activate"/>
          </object>
        </child>
        <child>
          <object class="GtkAction" id="mnuInsertDate">
<property name="name">mnuInsertDate</property>
<property name="label" translatable="yes">Insert Date/Time</property>
            <signal handler="on_mnuInsertDate_activate" name="activate"/>
          </object>
        </child>
      </object>
    </child>
    <ui>
      <menubar name="mnuMain">
        <menu action="mnuFile">
          <menuitem action="mnuNew"/>
          <menuitem action="mnuOpen"/>
          <menuitem action="mnuSave"/>
          <menuitem action="mnuSaveAs"/>
          <separator/>
          <menuitem action="mnuQuit"/>
        </menu>
        <menu action="mnuEdit">
          <menuitem action="mnuUndo"/>
          <menuitem action="mnuRedo"/>
          <separator/>
          <menuitem action="mnuCut"/>
          <menuitem action="mnuCopy"/>
          <menuitem action="mnuPaste"/>
          <menuitem action="mnuDelete"/>
          <separator/>
          <menuitem action="mnuSelectAll"/>
          <menuitem action="mnuInsertDate"/>
        </menu>
      </menubar>
    </ui>
  </object>
  <object class="GtkWindow" id="MainWindow">
<property name="visible">True</property>
<property name="title" translatable="yes">EdGtk+</property>
<property name="window_position">GTK_WIN_POS_CENTER</property>
<property name="default_width">750</property>
<property name="default_height">550</property>
    <signal handler="on_MainWindow_delete_event" name="delete_event"/>
    <child>
      <object class="GtkVBox" id="vbox1">
<property name="visible">True</property>
<property name="border_width">1</property>
        <child>
          <object class="GtkMenuBar" constructor="uimanager1" id="mnuMain">
<property name="visible">True</property>
          </object>
<packing>
<property name="expand">False</property>
          </packing>
        </child>
        <child>
          <object class="GtkToolbar" id="tlBar">
<property name="visible">True</property>
<property name="icon_size">GTK_ICON_SIZE_SMALL_TOOLBAR</property>
            <child>
              <object class="GtkToolButton" id="tlbNew">
<property name="visible">True</property>
<property name="label" translatable="yes">New</property>
<property name="stock_id">gtk-new</property>
                <signal handler="on_tlbNew_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkSeparatorToolItem" id="tlbsep1">
<property name="visible">True</property>
              </object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton" id="tlbOpen">
<property name="visible">True</property>
<property name="label" translatable="yes">Open</property>
<property name="stock_id">gtk-open</property>
                <signal handler="on_tlbOpen_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton" id="tlbSave">
<property name="visible">True</property>
<property name="label" translatable="yes">Save</property>
<property name="stock_id">gtk-save</property>
                <signal handler="on_tlbSave_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton" id="tlbSaveAs">
<property name="visible">True</property>
<property name="label" translatable="yes">Save As</property>
<property name="stock_id">gtk-save-as</property>
                <signal handler="on_tlbSaveAs_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkSeparatorToolItem" id="tlbsep2">
<property name="visible">True</property>
              </object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton" id="tlbUndo">
<property name="visible">True</property>
<property name="label" translatable="yes">Undo</property>
<property name="stock_id">gtk-undo</property>
                <signal handler="on_tlbUndo_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton" id="tlbRedo">
<property name="visible">True</property>
<property name="label" translatable="yes">Redo</property>
<property name="stock_id">gtk-redo</property>
                <signal handler="on_tlbRedo_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkSeparatorToolItem" id="tlbsep3">
<property name="visible">True</property>
              </object>
<packing>
<property name="expand">False</property>
<property name="homogeneous">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton" id="tlbCut">
<property name="visible">True</property>
<property name="label" translatable="yes">Cut</property>
<property name="stock_id">gtk-cut</property>
                <signal handler="on_tlbCut_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton" id="tlbCopy">
<property name="visible">True</property>
<property name="label" translatable="yes">Copy</property>
<property name="stock_id">gtk-copy</property>
                <signal handler="on_tlbCopy_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
            <child>
              <object class="GtkToolButton" id="tlbPaste">
<property name="visible">True</property>
<property name="label" translatable="yes">Paste</property>
<property name="stock_id">gtk-paste</property>
                <signal handler="on_tlbPaste_clicked" name="clicked"/>
              </object>
<packing>
<property name="expand">False</property>
              </packing>
            </child>
          </object>
<packing>
<property name="expand">False</property>
<property name="position">1</property>
          </packing>
        </child>
        <child>
          <object class="GtkScrolledWindow" id="scrolledwin">
<property name="visible">True</property>
<property name="can_focus">True</property>
<property name="hscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
<property name="vscrollbar_policy">GTK_POLICY_AUTOMATIC</property>
            <child>
              <object class="GtkTextView" id="textview">
<property name="visible">True</property>
<property name="can_focus">True</property>
              </object>
            </child>
          </object>
<packing>
<property name="position">2</property>
          </packing>
        </child>
        <child>
          <object class="GtkStatusbar" id="statusbar">
<property name="visible">True</property>
<property name="spacing">2</property>
          </object>
<packing>
<property name="expand">False</property>
<property name="position">3</property>
          </packing>
        </child>
      </object>
    </child>
  </object>
</interface>

the program generates the following code:

#!/usr/bin/env python

# This file was gererated on 24/09/08 with [gtkBuild2Py 0.1]

import sys
try:
    import pygtk
    pygtk.require("2.0")
    import gtk
except:
    sys.exit(1)

class pyGtkApp(object):
    """  write your application info here  """

    def __init__(self):
        builder = gtk.Builder()
        builder.add_from_file("editor.xml")

        #============== Application Widgets List ==============#
        #  You  can safely delete  from  the list below any    #
        #  widget  that  you won't use in  your code, i.e a    #
        #  label that doesn't change it's text, etc.           #
        #  Only  widgets that you reference them in your code  #
        #  need to be in the list. Leaving them here of course #
        #  doesn't have any effect.                            #
        #======================================================#
        self.MainWindow = builder.get_object("MainWindow")
        self.actiongroup1 = builder.get_object("actiongroup1")
        self.mnuCopy = builder.get_object("mnuCopy")
        self.mnuCut = builder.get_object("mnuCut")
        self.mnuDelete = builder.get_object("mnuDelete")
        self.mnuEdit = builder.get_object("mnuEdit")
        self.mnuFile = builder.get_object("mnuFile")
        self.mnuInsertDate = builder.get_object("mnuInsertDate")
        self.mnuMain = builder.get_object("mnuMain")
        self.mnuNew = builder.get_object("mnuNew")
        self.mnuOpen = builder.get_object("mnuOpen")
        self.mnuPaste = builder.get_object("mnuPaste")
        self.mnuQuit = builder.get_object("mnuQuit")
        self.mnuRedo = builder.get_object("mnuRedo")
        self.mnuSave = builder.get_object("mnuSave")
        self.mnuSaveAs = builder.get_object("mnuSaveAs")
        self.mnuSelectAll = builder.get_object("mnuSelectAll")
        self.mnuUndo = builder.get_object("mnuUndo")
        self.scrolledwin = builder.get_object("scrolledwin")
        self.statusbar = builder.get_object("statusbar")
        self.textview = builder.get_object("textview")
        self.tlBar = builder.get_object("tlBar")
        self.tlbCopy = builder.get_object("tlbCopy")
        self.tlbCut = builder.get_object("tlbCut")
        self.tlbNew = builder.get_object("tlbNew")
        self.tlbOpen = builder.get_object("tlbOpen")
        self.tlbPaste = builder.get_object("tlbPaste")
        self.tlbRedo = builder.get_object("tlbRedo")
        self.tlbSave = builder.get_object("tlbSave")
        self.tlbSaveAs = builder.get_object("tlbSaveAs")
        self.tlbUndo = builder.get_object("tlbUndo")
        self.uimanager1 = builder.get_object("uimanager1")
        self.vbox1 = builder.get_object("vbox1")
        #================ End of Widgets List =================#
        builder.connect_signals(self)
        self.MainWindow.show()

    def on_MainWindow_delete_event(self, widget, event, data=None):
        gtk.main_quit()

    def on_tlbCopy_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_tlbCut_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_tlbNew_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_tlbOpen_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_tlbPaste_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_tlbRedo_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_tlbSaveAs_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_tlbSave_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_tlbUndo_clicked(self, widget, data=None):
        print "this function is not implemented yet"

    def on_mnuCopy_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuCut_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuDelete_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuInsertDate_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuNew_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuOpen_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuPaste_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuQuit_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuRedo_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuSaveAs_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuSave_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuSelectAll_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def on_mnuUndo_activate(self, menuitem, data=None):
        print "this function is not implemented yet"

    def main(self):
        gtk.main()

if __name__ == "__main__":
    app = pyGtkApp()
    app.main()

IMPORTANT NOTE: Glade3 comes with two ‘major’ bugs in it. The first one is about Menus. For a short description of the bug and how you can make your Menus in glade3, just read this tutorial from Micah Carrick’s blog. (scroll down to the middle of the page to read it.)

The second bug is about ComboBoxes. You put a ComboBox in your window, you populate a list in it, and then you run your program and you see that there aren’t any items inside the ComboBox and you get also an Error about NoneType Object, meaning that for the program there isn’t a ComboBox in the window. To solve this, I’m afraid you must search yourself for the solution.
I just mentioned these two bugs here for you to know whom to blame if you ever face them.

Hope you’ll find this program usefull.

And remember, for bugs, feedback, recomendations or whatever, send either an e-mail or write a comment here.
Happy coding!

2 comments so far

  1. RockDoctor on

    Looks like an interesting project – would love to have a copy of the code, but I see you removed it. Can you email it to me?

    Thanks

  2. Tommy Brunn on

    Why did you remove the files? Since the code is under the GPL, why not let someone else continue working on it? I would love to see a copy of it, so if you would, please send the code to me via email.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: