On Thu, Jan 16, 2003 at 05:36:21PM -0200, Johan Dahlin wrote:
> Hi fellow PyGTK users.
>
> I recently checked in a patch to fix bug 102650,
> It changes the behavior of glade.XML.signal_autoconnect slightly;
> First of all you can now pass in any kind of Mapping objects, eg all
> objects that support __getitem__, dictionary like items. But that's not
> all.
> You can also pass in a normal instance, and if it doesn't have an item
> like the signal handler, it will fallback to using the _attribute_ of
> the current object.
> What does this mean for you?
> Let me show you with the following example:
>
> class MyProgram:
> def __init__(self):
> wtree = glade.XML('MyProgram.glade')
> wtree.signal_autoconnect(self)
> def on_button_show_clicked(self, button): ...
> def on_button_edit_clicked(self, button): ...
> def on_button_copy_clicked(self, button): ...
> def on_button_move_clicked(self, button): ...
>
> So instead of doing:
>
> wtree.signal_autoconnect(
> {'on_button_show_clicked': self.on_button_show_clicked,
> 'on_button_edit_clicked': self.on_button_edit_clicked,
> 'on_button_copy_clicked': self.on_button_copy_clicked,
> 'on_button_move_clicked': self.on_button_move_clicked})
>
> you just pass in an object, self, an instance in this case.
>
> wtree.signal_autoconnect(self)
>
> And glade connects everything for you.
Does it walk the class hierarchy in search for handlers, or does it
stop at self?
> I imagine that every glade user on the list can remove tons of lines
> from their programs now :)
>
> Unfortunately this is only for pygtk 1.99.x.
> But hey, if you're still using 0.6.x it's time to upgrade soon anyway.
I am using the following class as a base for my widgets/controllers:
---cut here---cut here---cut here---
import gtk
import gobject
import gtk.glade
import new
import types
import re
class Controller:
#This method loads the XML file and autoconnects the signals
def __init__(self, gladeFile, windowName):
# save to be used later when instantiating child windows
self.gladeFile = gladeFile
# construct the window
self.widgets = gtk.glade.XML(gladeFile, windowName)
callbacks = {}
# look at methods named "on.*"
reON = re.compile("^on")
classesInspected = {}
classesToInspect = [self.__class__]
# find and store methods as bound callbacks
while classesToInspect:
currentClass = classesToInspect.pop()
if not classesInspected.has_key(currentClass):
classesInspected[currentClass] = 1
for cc in currentClass.__bases__:
classesToInspect.append(cc)
classMethods = currentClass.__dict__
for method_name in classMethods.keys():
if reON.match(method_name) and not callbacks.has_key(method_name):
method = classMethods[method_name]
if type(method) == types.FunctionType:
callbacks[method_name] = new.instancemethod(method,
self,
currentClass)
# autoconnect
self.widgets.signal_autoconnect(callbacks)
---cut here---cut here---cut here---
Cheers,
florin
--
"NT is to UNIX what a doughnut is to a particle accelerator."
msg05259/pgp00000.pgp
Description: PGP signature
