Tkinter & Python: how to get the return of a call associated to an event on my gui

Egbert Bouwman egbert at bork.demon.nl
Sat Jun 22 19:57:15 CEST 2002


A callback may do something, but it does not return anything,
at least not something you may use. I suppose that you need 'global',
ie an export of a name from within a function (the local namespace)
to the surrounding global one: the namespace of the module in which 
the function is defined. I think the line 'global result' in your
callback will help.

As long as you stay within one class-instance, you can solve it in 
another way: let your callback produce a 'self.result', which is 
available in the whole class-instance.

Things become interesting when you have two classes, A and B,
and A needs the data that will be collected by a gui in B.
I call it interesting because I am still struggling with it.

A and B should agree on the layout of the data structure that goes
from B to A, but how B collects these data, from which entries or
canvases, is entirely his own business. In the same way: B knows 
nothing of the way A handles these data.

First the solution, and then some explanations.
    
    class B:
        def __init__(self,callback=None):
            # build a gui ...
            bouton = Button( ....., command=callback)
            bouton.grid( ...)
           
        def retour(self):
            # collect the gui-data in retour_list[],
            # and probably close this b-gui.
            return retour_list
        
    class A:
        def __init__(self):
            b = B( ..., callback=self.sign)
            
        def sign(self,event=None):    
            self.data_list = b.retour()
            process_data()
            
        def process_data(self)
            # do something usefull with self.data_list
            # exit (button) the application
            
Within the usual Tkinter loop you start this with:
    a = A()

A passes in an argument the name of a callback to B.
This callback ('sign') is a method of A.
The chain of activities is started by the button-push in B
that activates the callback in A.
The callback sends a message ('retour') to B to collect 
and return the data, and A processes them.

How B collects the data is hidden in 'retour', and what A
does with them is hidden in 'process_data'.

Now I have two questions:

am I making things much too complicated, or
does some standard pattern solve this problem in a better way ?

egbert
-- 
Egbert Bouwman - Keizersgracht 197 II - 1016 DS  Amsterdam - 020 6257991
========================================================================





More information about the Python-list mailing list