Daniel Kornhauser wrote:
> I've stated to document some of pygtk.
>
Great!

I have been thinking to start the task myself, but then decided to
give up 'cause... <segmentation-fault-in-my-brain>.

Anyway, i was thinking that it would be nice to make some
*interactive* examples, like the Python tutorial by Guido van Rossum
does.

The interactive console is one of the features that make Python so
easy to learn and play with, so i think it could be a great way to
"teach" GTK+/GNOME, too.

Think about the classic "hello world" example. If the reader had an
interactive pygtk console, you could say:


  To use the GTK libraries, we have to load the appropriate
  python module in the usual way first.
  
  > from gtk import *
  
  Now we want to fire up a window. This is done by creating an
  instance of class GtkWindow, and then show()ing it.
  
  > window = GtkWindow()
  > window.show()
  

At this point, with just three lines of code, the reader will see
his/her first GTK+ widget show up!

Hey, i'm having fun! so i'm going to finish my example.


  OK, we have a window. But that's empty. Let's put something in
  it, say a button.
  
  > button = GtkButton("Hello World")
  > button.show()
  
  You don't see your new button? Try to add the following.
  
  > window.add(button)
  
  Without that, GTK wouldn't know where to place the button. Now
  that you see the button, try to click on it. Of course, nothing
  happens, because we haven't defined any action associated with
  the button click. Yet.
  
  To do it, first we need to define a python function that will 
  be called when the event occurs.
  
  > def say_hello(button):
  >     print "Hello, World!"
  
  Now that we have a callback function, we can associate it with
  the button.
  
  > button.connect("clicked", say_hello)
  
  OK. Click on the button and pretend to be surprised. :)


And the user will see the "Hello, World!" message printed out on
its console each time he/she press the button.

The main problem is, this interactive approach doesn't work on a
standard xterm python shell. Since the GTK main loop isn't active,
the widgets aren't actually drawn, and even if you force it to
show them issuing a

  > while events_pending():
  >     mainiteration()

statement from time to time, the widgets aren't really working
(you can't click on a button, for instance).

But it *does* work on a console written in PyGTK, like the one
coming with pygtk distribution as gtkcons.py on examples/ide,
or the one i posted to this list a couple of weeks ago.

The same interactive approach could be very useful to learn the
basic working of each widget. At least, that's the way i like.

Time over. Tell me what you think, and please autocorrect my 
English mistakes while you read the above.

Ciao!

-- Mirko Nasato
To unsubscribe: echo "unsubscribe" | mail [EMAIL PROTECTED]

Reply via email to