## 2.3 Views

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:

- `BaseView`, which is a View with a toplevel window, in which you create and organize the widgets using PyGTK classes and commands.
- `BaseView`, which allows widgets to be defined using Glade and loaded in runtime using gazpacho or libglade. This allows widgets to be easily changed and rearranged visually, without the code needing to be changed.
- `SlaveView`, which is a View with a toplevel widget that is not a window. SlaveViews are designed to be embedded into other Views, usually representing a part of an interface that must be reused in multiple places in your application. Kiwi makes it easy to reuse that complicated form that you created in multiple windows of your app.

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

- `toplevel`, which defines which widget is the toplevel widget for that view. For a BaseView, this is the window you want to display. We pass in the gtk.Window we just created. The toplevel widget is used for setting focus and showing the View.
- `delete_handler`, which defines which function should be called when the window is deleted (this happens when the almost ubiquitous "X" button offered by window managers is clicked, or `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.

#### Footnotes

... parameters2
  Note that I used positional parameters here just to make things clear; they are in the proper order and the whole call could be `Views.BaseView.__init__(self, win, gtk.main_quit)`.