next up previous contents
Next: Custom widgets support Up: Views Previous: Constructor   Contents


A widgets container

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

from gtkmvc 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 constructor, that will delay the view registration.

from gtkmvc import View

class MyView (View):
  def __init__(self, controller):
    View.__init__(self, controller, 'test.glade', register=False)
 
    # ad-hoc widgets
    v['vbox_widget'] = gtk.VBox()
    ...

    controller.register_view(self) # this is required!
    return
  pass

Another important mechanism provided by the class View is the signals auto-connection. By using Glade users can associate to widget's 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.



Roberto Cavada 2008-08-26