Skip navigation.

Learn how to program cool GNOME and Gtk+ apps using PyGTK

GNOME
GNOME

A new update to the spanish translation of the superb and recently updated PyGTK tutorial has been released.

The PyGTK tutorial makes even easier and funnier learning how to program graphical applications using Python and Gtk+ that work seamlessly on GNU/Linux, *BSD or MS-Windows.

These updates include new information about features and widgets added to Gtk+ 2.4.

The tutorial begins with the basics to smoothly get into the more complex widgets. All this is achieved using a number of nicely comented code examples and insightful explanations. Though some knowledge of Python is needed to go through it, no previous GTK+ experience is required.

PyGTK, when used with gnome-python, also allows writing full featured applications for the GNOME Desktop.

As always, it can be found at www.pygtk.org, the reference site for PyGTK lovers.

And you... have you realized that, with 2005, the time has come to start learning and enjoying PyGTK?.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

autotools

The biggest problem with writing pygtk programs are the FSCKING AUTOTOOLS!!!!
Dammit, it takes longer then actually writing the freaking program!

I tried to read this

I just tried to read the code from this site:

http://www.pygtk.org/pygtk2tutorial/app-CodeExamples.html

but it's a pain. Is there a way to mark blocks with delimiters somehow?
Any help appreciated.

all modern code is indented

All modern code is indented. Since you were going to indent anyway, delimiting blocks with syntactic sugar just causes useless syntax errors.
-russ

Open gnome terminal and run:

Open gnome terminal and run:

sed 's/^......//' >scribblesimple.py

Then hightlight code in browser window (copy) and click middle button on terminal window (paste).

Then key and D in the terminal window.

The above will strip the first 6 characters and make a file called scribblesimple.py.

Examples

You can use vim's column mode, and I'm sure emacs can do it too :)

well the auto HTML broke the

well the auto HTML broke the above comment;

Then key <enter> and <ctrl>D in the terminal window.

Bug

It's a long standing Python bug.

As previously mentioned by another poster, please do not offer your opinion as fact. It is _not_ a bug. It's a feature. And I'm one of those ones who wouldn't have it any other way. Whenever I have to write some perl or C I get outright annoyed at having to manually put in lexical blocks.

Give python's syntax a chance with an honest two weeks or a relatively large project. Most people I've talked to agree with me by the end of it.

python bug: python is slow by Anonymous George

If you want to pitch Ruby or

If you want to pitch Ruby or Perl, the least you could do is be honest about Python. It's not a "bug", it's the way the language works, and many, many people are happy about it.

no ruby, no perl, no python by Anonymous George
Yeah, sure by Anonymous George

It's NOT a long-standing bug,

It's NOT a long-standing bug, and not even an obvious bug. It IS the way the language works. I like it, and many many people also like it.

Can you mark blocks by indent

Can you mark blocks by indentation in ruby or perl?

Please, all but Python by Anonymous George

Python is a real language

First, everyone running Fedora has Python installed. It's simply not true that "Not many people have Python installed".

Second, Python is a real programming language. I accept that you don't like it, but your dislike of Python has nothing to do with its stature as a real programming language.

Third, sure, Python could be buggy, but so could GCC.

So? Everyone has it installed by Anonymous George

Re: Please, all but Python

What utter crap your statement is. Out of the main four or five distributions tell me one that doesn't install Python by default. They all have it. Ubuntu, Fedora, Mandrake, Debian, Suse.

I don't use Python at the moment but there is nothing wrong with programming in it.

Debian doesn't install Python by default

Debian doesn't install Python by default. At least it is not installed on my most recent Sarge, and I am sure I have not "apt-get remove"d it.

-knipknap

Yes it is

Debian does install python very very early, actually. If you do a bare install (ie, no software, just base kernel, GNU-utils, etc) then you don't get python, but I'd be surprised if you can get a usable desktop (or server) running without it.

bleh

C++ isnt a real programming language - its a sad, pathetic, verbose joke. OCaml and Haskell are real programming languages. There is nothing wrong with writing Gnome programs with Python. It provides a good introduction to the Gnome libraries to people who wouldnt otherwise be interested, and PyGTK is a GREAT tool for rapidly constructing useful graphical utilities.

Python is very common, and the word "questionable" is not at all applicable. Python is a solid language with good syntax and a powerful dynamic virtual machine. If you think C++ code is more readable than Python code, you either suffer from a serious mental deffeciency or you dont know python.

re:bleh by Anonymous George
Blah by Anonymous George
re: blah by Anonymous George

*Correct* Blame

I don't know about those two programs, but I do know that there is much to consider.
1) The libraries used by the program are slow (for example abusing the list view, or other such things in gtk or X)
2) Algorithmic slowdown - that a program is slow because of a bad algorith (say using polling instead of some kind of update signal, like I believe the first gdesklets version did)
3) Known optimisation law (I can't recall the exact name) - If, for example, a program is only 10% slow because of python (the other reasons for its slowy-ness are due to other reasons) then making the program not use program could give, f.e., 50% improvement for that part, you will only get a total improvement of 5% (math might not be exact). In other words, improve the slow parts, improving anything else is a waste of time.

So while Python is slower then a compiled language, since most programs, especially GUI ones, do not do intensive CPU work but instead do most of thier work by either waiting for input (from the user, for the files to become readable by the OS, for network input, etc...), or inside C libraries (like the gtk library, or the libc library), most programs arn't that affected by the fact that the program is written in python.

Note: the fact that there is a bug in a program (is a logical bug, or say an improper usage of python) that causes it to be slow, dosn't mean its python's fault.
Like some of the slowdowns in gnome-terminal that people kept blaming pango, in the end it turned out there was some serious speedups doable in the code...

Which brings us to our final conclusion. Don't complain about speed unless you have the numbers to prove it...

Your reasoning is right but..

I'm not gonna spend time benchmarking this, or so, since I have better work to do and the feeling is enough for me to search an alternative for Nicotine. The problem is that when i fire it up and go to sleep or work and i come back my computer is using all its resources. I get a huge load average, all my RAM is full, my swap is quite full, my CPU is using 100% -- all for Nicotine. I've never experienced such a thing with a simple P2P application. I'm just not using Gdesklets at all which I see as a toy anyway. I noticed this while i tried it out and i guarantee you i'm not the only one who notices the slowliness. Even people who have considered moderately fast x86's complain about this. You're still right though; its not necessarily Python.

I know i am damn right on spot with one thing though:

So while Python is slower then a compiled language

Which means its not very scalable. Which is the f*cking reason I believe Nicotine is slow (it doesn't scale while it has to) and why MySQL/PHP doesn't scale well (compared to e.g. Tomcat/JSP). I'm just noticing that of all the programs i know, comparable programs which are programmed in a compiled language roughly always seem to be faster -- right, seem. So i tend to evade big programs programmed in e.g. Python much as i evade Swing whereas i use Perl for sysadmin-related tasks at work. Why? I don't need scalability in such scripts. Small and flexible are the keywords there. :-)

I'm looking for a linux distr by Anonymous George

Nicotine and gdesklets

For Nicotine I have to agree - long list views always caused utter slowness.
With gdesklets I'm not sure whether the performance problems are Python related though. I rather had the impression that it has a major memory leakage or something, at least it seemed to be getting slower the longer you run it. I never really got into the issue though and I don't want to install Python to `top` it now.

I'd be happy to see new progr

I'd be happy to see new programmers, really, but if you plan on doing this please, please use a real programming language instead of Python.

Python is a real programming language: it is a means for you to communicate instructions to the computer.

Not only that, but Python is a particularly efficient one, in terms of development time. Learn Python, and you'd be hard-pressed to find another language which lets you do the same stuff in less time. Yes, the API is unstable and that sucks.

Just this week I had to create a small RPC server (a simplified Battle.NET clone). I decided to use Python. One hour later, I had a perfectly functional program, fulfilling all requirements, in 42 lines of (well-spaced) code. My test Java client (which does an equivalent amount of work) is well over three times longer, and it's certainly less legible and malleable.

I've tried GUI design in several languages. For rapid application development, PyGTK+Glade is at the same level as Visual Basic -- that is, many times faster than other programming languages and/or toolkits.

And recently we made Python bindings for Epiphany (so you can make extensions). The end result? Our 331-line sample extension in C can be replaced by a Python script... 8 lines long. Not to mention, we can embed a Python console in Epiphany, since it's an interpreted language. It would be impossible to embed a C console.

PyGTK is an absolutely fantastic tool. Python is a great language for many tasks (especially prototyping). Why write a message for the express purpose of complaining about a tool? If you don't like it, don't use it. But you will never convince anybody that Python is useless. It's not the right tool for every job, but it's certainly the right tool for many jobs. And PyGTK is an absolutely fantastic set of bindings.

vb vs. python

"For rapid application development, PyGTK+Glade is at the same level as Visual Basic"

Except Python has much better syntax, a better object model, and its distributed under a better license. :-)

This is the first I've heard of Python bindings for Epiphany. Definitely a cool idea. I wish that other Gnome people were more receptive to Python scripting support. Metacity could certainly benefit from python power.

VB Vs Python Vs Pascal by Anonymous George

Extensions for Metacity.. tha

Extensions for Metacity.. that would be freaking awesome :-)

Python _IS_ installed by default

Python is available in most distributions by default. In fact, RedHat and Fedora cannot function well without Python (redhat-config-* tools are written in Python). Even other distros like Mandrake does have python installed by default. Ubuntu is not only Gnome centric, it also has many python packages installed despite the interpreter itself.

Python is a great to language to program with. You will be much productive in Python than C/C++.

Read why you should learn Python

--
Sridhar Ratna

And in Gentoo, Portage is wri

And in Gentoo, Portage is writenn in Python.

Right

Ever noticed how getting the Portage DB takes a lot of time? I mean, writing the cache? When i compare that to APT which ain't fast either (especially with lots of I/O!), APT is at least significantly faster than Portage. IIRC the first APT was written in Perl but was rewritten in C for speed considerations and for Perl independence.

Not exactly by Anonymous George

Nonsense

Throughout the comments on this story you've been trolling, rolling out the common inane FUD that people use to disparage Python.

However, I have to reply to this one as it truly is risible. Python's syntax is extremely clear and it illustrates many of the fundamental concepts used in both functional and object-orientated programming. It does so in a manner that is far more comprehensible than any of the languages you mention. Hence the reason that many describe Python as "high-level" pseudo-code and universities are increasingly using Python in CS101 courses to introduce the core ideas of programming. They do this precisely because, as most people would know, learning one language should make learning other languages relatively simple.

Python may have some flaws relating to performance and dependency management, but syntactically many would regard it as exemplary. But you're trolling, so I'm sure you already know this.

?

How is learning C "learning for every language"? The structure of C is similar to such useless languages as C# and Java. When a new programmer learns Python, they get a good introduction to object oriented programming concepts with a simple and graceful syntax. Once a programmer learns Python, its easy for them to learn a nice, efficient language like Pascal or Eiffel.

msec?

Huh? Isn't msec a Python program? And msec is in Mandrake by default.

C++ tutorial

I second this. In fact, the Python API is pretty unstable, lacking backward compatibility, so that you'll need multiple Python interpreters installed in parallel to run Python scripts that were written for older interpreter versions. Too weird, IMO.

gtkmm was also my first thought. A great gtkmm tutorial is here:
http://www.gtkmm.org/docs/gtkmm-2.4/docs/tutorial/html/index.html

Pretty slick, and if you'll ever need help you can try the mailing list:
http://mail.gnome.org/mailman/listinfo/gtkmm-list

Either way, have fun.

Really ???

Oh, interesting.
Can you point something you can do with Python 2.0 (October 2000) that you can't do in Python 2.4 (November 2004) ?
I feel the necessity to see this "lack" with my eyes.

People can't imagine the market Python and Ruby are gaining until they see trolls whining, really :)

ngw

strange

Everyone is blaming Python for "lack of backward compatibility" and API unstability but nobody can give an example.
Uhm, probably nobody knows what "backward compatibility" means.
lol.

ngw

Please, Python more than anything else

Not many people have Python installed --

Python is almost standard on any linux install now. Just like Perl is too. All the major distrobutions include it. Most of them also now include PyGTK.

Python is more than a questionable script interpreter, it is powerful and can do almost everything that you would need it to. What it cant do, would often be something that you'd be insane to not use C.

Please people, give python a go, you'll love it, it's for everybody's happiness, including your own. Please.

>Please people, give python a

>Please people, give python a go, you'll love it, it's for everybody's happiness, including your own. Please.

I can totally agree with this.

I have never write a python program but today i was unhappy with gnome-blog that everytime i press the 'Return'-key i get a whole paragraph and can't create a simple newline.
Therefor i have look in the python sourcecode and have change this behavior in a few seconds (even if i have never used python before). Than i have started gnome-blog again and it simply works :)
Would it be an C program or something simular i would have to compile it and create a new deb-packet to install it in a clean way on my debian system. Python have made it possible to simple change something by editing one file.

In the next day i will defenetly give python a try. I think at least for small and usefull tools, python is defenetly a good option.

Python standard?

Hmmm, I know only two programs using that (gdesklets and that blog applet whose name i have forgotten), let alone having seen Python installed somewhere. I'm the only person I know who has tried Python out. There are probably more apps out there somewhere, but nothing particulary popular or important.

Really, I don't think it's anywhere near Perl's popularity. That's not to say I recommend people to write GTK applications using Perl. I don't. (but at least it's more API stable than Python.)