Howto generate a metafunction? [Python 1.5.2]

Greg Ewing see_reply_address at something.invalid
Wed Oct 16 00:01:06 CEST 2002

Kim Petersen wrote:

> class MListbox(Frame):
>    def __init__(self,master,count=1,cnf={},**args):
>        Frame.__init__(self,master,cnf)
>        self.listboxes=[]
>        for i in range(count):
>           self.listboxes.append(Listbox(self,command=self._docommand))
>           self.listboxes[-1].grid(row=0,column=i)
>        self.yview=self.Metafun(Listbox.yview)
>        self.yview_fraction=self.Metafun(Listbox.yview_fraction)
>        ...
>    def Metafun(self,fun):
>        # How do i generate this one - to apply this function to every
>        # element in self.listboxes?

It looks like you're trying to synchronise the scrolling of
several listboxes, right?

When I needed to do this a while ago, I created the
following class:

   class ScrollMultiplexer:

     def __init__(self):
       self.clients = []

     def add(self, c):

     def xview(self, *args):
       for c in self.clients:
         apply(c.xview, args)

     def yview(self, *args):
       for c in self.clients:
         apply(c.yview, args)

You would use it like this:

   class MListbox(Frame):
     def __init__(self,master,count=1,cnf={},**args):
        # Create a ScrollMultiplexer
        mux = ScrollMultiplexer()
        # Create a scroll bar and connect it to the ScrollMultiplexer
        scrollbar = Scrollbar(self, orient = 'v', command = mux.yview)
        for i in range(count):
           listbox = Listbox(self,command=self._docommand)
           # Add the listbox to the ScrollMultiplexer
        # Finally, connect *one* of the listboxes back to the scroll bar
        # (it doesn't matter which one)
        self.listboxes[0].configure(yscrollcommand = self.scrollbar.set)

In other words, the ScrollMultiplexer sits in between
the scrollbar and the listboxes, pretending to be a
sort of fake listbox, distributing the scrolling
commands from the scrollbar to all the real

Greg Ewing, Computer Science Dept,
University of Canterbury,	
Christchurch, New Zealand

More information about the Python-list mailing list