A View is a class that defines how a single window will look. Differently from most frameworks, Kiwi requires almost no infrastructure code to create a trivial app; Kiwi allows you to create a view and run it by itself, with no attached handlers, controller or model. (Whether or not this is a very useful app is another matter, but let's get you started on it before you criticize me.)
There are a number of different Views available for Kiwi, which live in the View module:
To start off, the easiest way to demonstrate a View is to create a very simple one that does nothing but present a fixed label on the screen. This first example will use GTK+ widgets directly, just to show how they fit in (it's available in the Kiwi tarball in Kiwi/examples/HeyPlanet/hey.py).
So what does the example do? To start off with, it defines a view class that inherits from BaseView, the View with a toplevel window. BaseView should always be subclassed, since it requires you to to create the widgets programatically. You should instantiate and assemble PyGTK widgets in the constructor before running BaseView.__init__(). We initialize BaseView with two parameters2
del(window)
is
invoked). We are passing in mainquit, which breaks the mainloop we are
running in, and allows the application to exit.
If you didn't pass in a delete handler, closing the window would leave the python interpreter hung in the mainloop. What mainloop? Read on.
Right after we instantiate our view class, we call a special method in
it: show_all_and_loop()
. This method calls show() on all the
widgets under the toplevel widget including it (in our cases, it will
show the label and the window), and runs the GTK+ event loop (or
mainloop). The mainloop is a fundamental aspect of GUI programming: when
it is invoked, it assumes control of the normal flow of execution and
continuously processes events that are generated by user interaction
with the widgets. You can read more about the mainloop in the mainloop
section of
GTK+/Gnome Application Development.
show_all_and_loop()
(and its variant show_and_loop()
)
returns a value upon exit. This return value corresponds to the View's
retval
instance variable, and you can use self.retval
to
return any value you wish. In our example the use was rather trivial,
but retval
is convenient when opening a dialog: use
show_all_and_loop()
to run it; the dialog modifies its
retval
variable depending on a certain event (OK button was
pressed versus Cancel button, for instance), and the method will return
this value to the caller (which can then make a decision based on it).
Right now, the relevant aspect of the mainloop is that we will need to
break this loop when we quit our application, which for this app should
happen when you click on "X". This is why we are passing in
mainquit as delete_handler
: to stop the mainloop and
let the control go back to our code (which also ends right after, at
which time you are left staring at the console).
BaseView is the most basic type of view, and it's only worth so much explaining. The next section describes GladeView, which is a lot more interesting.
Views.BaseView.__init__(self, win, mainquit)
.