ARTICLE

From Old To New Python GTK

by | Tue 24 Jan 2012

I am a pretty terrible programmer. Anyone who has read my code can see that. Unfortunately, I tend to have lots of ideas about how we can use technology in different ways, hence why I write some code. Examples of this have included Lernid, Acire, RaccoonShow, and Jokosher.

Fortunately (or unfortunately, depending on your view), I have had Python and GTK to serve my needs here. Python, with it’s awesome batteries-included range of facilities and GTK as a simple yet flexible toolkit has allowed me to create implementations of the ideas that I have dreamed of. I started using these tools many years ago, and they have always provided a simple and effective toolset for me.



My preferred toolset of choice. One day…

Having not written any code for a while, I got the itch this weekend to start writing the [trophy helper app that I wrote about as part of the accomplishments system](https://archivedblog.jonobacon.com/2012/01/23/hacking-on-accomplishments/) spec that I created with Stuart Langridge and Daniel Holbach. I thought this would be a good opportunity to brush up on my skills, given that PyGTK is dead and the new world is instead the GIR approach to GTK. In a nutshell, this is where the language bindings basically match the C API for GTK thus reducing the need for people to maintain different language bindings.

Of course, this is a good thing: less work for volunteers in maintaining multiple-language support for GTK and a consistent API is good. Unfortunately, I found getting started with this new world a little more complex than I imagined.

From reading the documentation it suggested that all I needed to do was to import `Gtk` from `gi.repository` and instead of creating widgets with `gtk.` that they would be `Gtk.`. The docs suggested a few other lexical adjustments, but not much more than that. There is even a [pygi-convert.sh script](https://git.gnome.org/browse/pygobject/tree/pygi-convert.sh) that can convert older PyGTK code over to the new PyGI way. Unfortunately the script didn’t work for me, so I instead used it as a cheat-sheet for things that needed changing. Sadly, it seemed like some things were not covered in the script.

An example of this included when I was creating a `ListStore`. In PyGTK code I could add a `gtk.gdk.Pixbuf` to the ListStore for an icon, but I had a difficult time trying to figure out the new way to describe this. I tried `Gtk.gdk.Pixbuf` and `Gtk.Gdk.Pixbuf` but had no luck. Fortunately the awesome *Ryan Lortie* informed me that it needed to be `GdkPixbuf.Pixbuf`. Another example of this was `gtk.SORT_ASCENDING` in my original code and the new `Gtk.SortType.ASCENDING` in the new code. It seems like various functionality in GTK has been moved around and re-factored.

Unfortunately I could not find any documentation to help me with this. Sure, the C docs are available online, but I am not a C programmer; I am (in the most generous and understanding way) a Python programmer and where I previously had a pretty decent tutorial and reference guide to PyGTK, as a desktop app developer I no longer have these resources to help me. Even though I am not a fantastic programmer, I have written enough Python and GTK code to fumble my way through writing various apps, and if it stumped me as a relatively old hand, I wonder how a brand new developer would get on.



Pictured: old hand.

Now, this may sound a little critical, but it is not mean’t to be. I have tremendous respect for the GTK team, and I am hugely thankful to them for all their hard work. I am also thankful for the team that has worked on the GIR support so that multiple language support can be more efficiently provided. Thanks to all you folks for providing great tools that let a programming numpty such as myself be able to write Free Software.

I just wanted to share this because I feel like these tools are missing the final component: if we had a good solid set of reference documentation generated for each language (naturally, Python is the language I mainly care about), this would help novice and established developers use GTK more effectively. From my personal experience, my patience started wearing pretty thin when I felt like I didn’t have anywhere to find help as I navigated C documentation to try and figure out how the API fitted into my Python application. A good solid Python reference manual would have resolved this issue, and from what I understand, this could potentially be generated from the GIR files. Unfortunately, I don’t think I have the skills to help solve this problem, so I figured the best I could do was to share my story and see if anyone would be interested in helping to solve this problem.

If so, thanks in advance, and thanks again to the GTK team for all your hard work!

### Update

I found [this excellent documentation](https://readthedocs.org/docs/python-gtk-3-tutorial/en/latest/) after publishing this entry. This provides exactly the kind of documentation I was looking for. Thanks to anyone who helped contribute to this!

An invitation-only accelerator that develops industry-leading community engagement and growth via personalized training, coaching, and accountability...all tailored to your company's needs.

Want to read some more?

Happy Holidays

Happy Holidays

Just a quick note to wish all of you a happy, restful, and peaceful holidays, however and whoever you spend it with. Take care, folks, and I look forward to seeing you in 2015!

The Impact of One Person

The Impact of One Person

I am 35 years old and *people* never cease to surprise me. My trip home from Los Angeles today was a good example of this. It was a tortuous affair that should have been a quick hop from LA to Oakland, popping on BArt, and then getting home for a cup of tea and an...

Feedback Requested: Great Examples of Community

Feedback Requested: Great Examples of Community

Folks, I need to ask for some help. Like many, I have some go-to examples of great communities. This includes Wikipedia, OpenStreetmap, Ubuntu, Debian, Linux, and others. Many of these are software related, many of them are Open Source. I would like to ask your...

Ubuntu Governance Reboot: Five Proposals

Ubuntu Governance Reboot: Five Proposals

Sorry, this is *long*, but hang in there. A little while back I wrote [a blog post](https://archivedblog.jonobacon.com/2014/11/14/ubuntu-governance-reboot/) that seemed to inspire some people and ruffle the feathers of some others. It was designed as a...

Ubuntu Governance: Reboot?

Ubuntu Governance: Reboot?

For many years Ubuntu has had a comprehensive governance structure. At the top of the tree are the Community Council (community policy) and the Technical Board (technical policy). Below those boards are sub-councils such as the IRC, Forum, and LoCo councils, and...

Dealing With Disrespect: The Video

Dealing With Disrespect: The Video

A while back I wrote and released a free e-book called [Dealing With Disrespect](https://www.dealingwithdisrespect.com/). It is a book that provides a short, simple to read, free guide for handling personalized, mean-spirited, disrespectful, and in some cases,...