next up previous
Next: Observable Properties in details Up: Models, Controllers and Views Previous: Controller

View

User's views derives from base class View. This is the only part specific for the Pygtk graphic toolkit.

A View is always associated to a Controller (that gets with its constructor call). When the view is created, it register itself to the controller by calling method Controller.registerView.

A View is also associated to a set of widgets. In general, this set can be organized as a set of trees of widgets. Each tree can be optionally be generated by using the Glade application (see section 5.1).

The View contructor is quite much complicated:

 
def __init__(self, controller, glade_filename=None,
             glade_top_widget_name=None, parent_view=None, 
             register=True)

glade_filename
can be a string or a list of strings. In any case weach string provided represents the file name of Glade output. Typically each glade file contains a tree of widgets.

glade_top_widget_name
can be a string or a list of strings. Each string provided is associated to the parameter glade_filename content, and represent the name of the widget in the widgets tree hierarchy to be considered as top level. This let the user to select single parts of the glade trees passed.

parent_view
is the view instance to be considered parent of self. Generally this parameter is None.

register
is a flag used to delay view's registration. If your derived view class adds some widgets ``manually'' by creating on the fly them (see below), you want to delay the view registration (performed by the View class contructor) 'till all ad-hoc widgets have been actually created. Since the View's constructor must be called at the beginning of your derived view class, you can avoid the View constructor calling Controller.registerView by setting this flag to False. After your view class contructor built all the widgets, it is responsible for calling Controller.registerView to perform the registration. (This is definitly more complicated to explain than to understand...)

The View class also can be considered a map, that associates widget names to the corresponding widget objects. If file test.glade contains a Button you called start_button from within Glade, you can create the view and use it as follows:

from gtkmvc.view import View

class MyView (View):
  def __init__(self, controller):
    View.__init__(self, controller, ``test.glade'')
    return
  pass 

m = MyModel()
c = MyController(m)
v = MyView(c)

v['start_button'] # this returns a gtk.Button object

Instead of using only Glade files, sometimes the derived views create a set of widgets on the fly. If these widgets must be accessed later, they can be associated simply by (continuing the code above):

v['vbox_widget'] = gtk.VBox()
...

Typically the creation on the fly of new widgets is performed by the derived view contructor, that will delay the view registration.

Another important mechanism provided by the class View is the signal autoconnection. By using Glade users can associate to widget signals functions and methods to be called when associated events happen. When performs the registration, the View searches inside the corresponding Controller instance for methods to associate with signals, and all methods found are automatically connected.


next up previous
Next: Observable Properties in details Up: Models, Controllers and Views Previous: Controller
Roberto Cavada 2004-11-16