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 kiwi.ui.views 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 examples/framework/hey/hey.py).
#!/usr/bin/env python import gtk from kiwi.ui.views import BaseView class HeyPlanet(BaseView): def __init__(self): win = gtk.Window() win.set_title("I'm coming to London") label = gtk.Label("Anything to declare?") win.add(label) win.set_default_size(200, 50) BaseView.__init__(self, toplevel=win, delete_handler=self.quit_if_last) app = HeyPlanet() app.show_all() gtk.main()
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 gtk.main.quit, which breaks the main loop we are
running in, and allows the application to exit. Note that we're passing
in the function object itself, instead of the result of a function call.
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_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.
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 BaseView, which is a lot more interesting.
Views.BaseView.__init__(self, win, gtk.main_quit)
.