Saturday, May 21, 2011

Introspecting Into GtkWindow

GtkWindow is a basic widget class, commonly used in GUI's, so it's a good place to start with. You can always find more information on any GtkWidget subclass on the Gnome Developer's Documentation. But as I mentioned earlier, the documentation is for C programming and there is hardly any material for PyGI.
Also if you take a look at the documentation on GtkWindow, you will guess that not all methods are provided by the documentation. For instance the gtk_show_all() function is not included, because it is inherited from GtkWidget. As a beginner, it would be quite unfortunate to miss out on this function because it allows the GtkWindow object and all its children widgets (all the controls it includes) to be drawn onto the screen.
So another way to find out what other secrets the GtkWindow object holds, is to introspect the object with the dir() function. Because the GtkWindow object has a lot of attributes and methods, we get quite a big list in return. Let's try out and see:

In a Python Console, first import Gtk and then run dir() as follow:

>>> from gi.repository import Gtk
>>> for item in dir(Gtk.Window):
...     print item
 
As I promised, there are quite a few attributes and methods, so I won't list them here lest we'd waste space. But for the sake of introspection, we could add:

>>> for item in dir(Gtk.Window):
...     if 'show' in item:
...         print item
...
do_show()
do_show_all()
do_show_help()
get_no_show_all()
reshow_with_initial_size()
set_no_show_all()
show()
show_all()
show_now()
Now we observe gladly that the show_all() method is listed. Notice that, because it is a Python binding, it has not the same syntax as the C function:  gtk_window_show_all corresponds to Gtk.Window.show_all(). You could always look into the Gnome Developer's Documentation for more details. In theory, we could get help() or __doc__ to assist us further, but unfortunately we won't rub our hands in excitement with the little doc available:
>>> help(Gtk.Window.show_all)
Help on method show_all:
show_all(*args) unbound gi.repository.Gtk.Window method

>>> print Gtk.Window.show_all.__doc__
None

Update (25/05/11): the __gdoc__ property offers information:
>>> print Gtk.Window.__gdoc__
''' Object GtkWindow

Signals from GtkWindow:
  set-focus (GtkWidget)
  activate-focus ()
  activate-default ()
  keys-changed ()

Properties from GtkWindow:
  type -> GtkWindowType: Window Type
    The type of the window
  title -> gchararray: Window Title
    The title of the window
  role -> gchararray: Window Role
    Unique identifier for the window to be used when restoring a session
  resizable -> gboolean: Resizable
    If TRUE, users can resize the window
  modal -> gboolean: Modal
    If TRUE, the window is modal (other windows are not usable while this one is up)
...
... '''

No comments:

Post a Comment