# This is file view_no_glade.py
from gtkmvc.view import View
import gtk
class MyViewNoGlade (View):
def __init__(self, ctrl):
# The view here is not constructed from a glade file.
# Registration is delayed, and widgets are added manually,
# later.
View.__init__(self, ctrl, register=False)
# The set of widgets:
w = gtk.Window()
h = gtk.VBox()
l = gtk.Label()
b = gtk.Button("Press")
h.pack_start(l)
h.pack_end(b)
w.add(h)
w.show_all()
# We add all widgets we are interested in retrieving later in
# the view, by giving them a name. Suppose you need access
# only to the main window, label and button. Widgets are
# added like in a map:
self['main_window'] = w
self['label'] = l
self['button'] = b
# View's registration was delayed, now we can proceed.
# This will allow the controller to set up all signals
# connections, and other operations:
ctrl.registerView(self)
return
pass # end of class
The entire work is carried out by the class constructor. At the beginning base class View is called like in glade-based view class, but now parameter register is set to False, to delay the registration of the view within the controller. This to allow manual construction of the widgets set, that later during registration the controller will be able to access.
Following lines are used to build the widgets set, and to associate a few of them with string names.
Finally, last line calls method registerView of the controller, in order to at last allow the controller to know about this view.
Notice that here glade file has not been used at all. Nevertheless, a mixed solution where glade file(s) and manually constructed widgets sets is fully supported.