hitcounter.py
1 import hal 2 import glib 3 import time 4 5 class HandlerClass: 6 ''' 7 class with gladevcp callback handlers 8 ''' 9 10 def on_button_press(self,widget,data=None): 11 ''' 12 a callback method 13 parameters are: 14 the generating object instance, likte a GtkButton instance 15 user data passed if any - this is currently unused but 16 the convention should be retained just in case 17 ''' 18 print "on_button_press called" 19 self.nhits += 1 20 self.builder.get_object('hits').set_label("Hits: %d" % (self.nhits)) 21 22 def __init__(self, halcomp,builder,useropts): 23 ''' 24 Handler classes are instantiated in the following state: 25 - the widget tree is created, but not yet realized (no toplevel window.show() executed yet) 26 - the halcomp HAL component is set up and the widhget tree's HAL pins have already been added to it 27 - it is safe to add more hal pins because halcomp.ready() has not yet been called at this point. 28 29 after all handlers are instantiated in command line and get_handlers() order, callbacks will be 30 connected with connect_signals()/signal_autoconnect() 31 32 The builder may be either of libglade or GtkBuilder type depending on the glade file format. 33 ''' 34 35 self.halcomp = halcomp 36 self.builder = builder 37 self.nhits = 0 38 39 40 41 42 def get_handlers(halcomp,builder,useropts): 43 ''' 44 this function is called by gladevcp at import time (when this module is passed with '-u <modname>.py') 45 46 return a list of object instances whose methods should be connected as callback handlers 47 any method whose name does not begin with an underscore ('_') is a callback candidate 48 49 the 'get_handlers' name is reserved - gladevcp expects it, so do not change 50 ''' 51 return [HandlerClass(halcomp,builder,useropts)]