[Tutor] Creating arbitrary numbers of destinations. (GUI)

Alan Gauld alan.gauld at btinternet.com
Wed Dec 7 23:57:41 CET 2011


On 07/12/11 15:16, Mic wrote:

>> OK, that should work except you haven't set the command value to the
>> function that creates the seat booking window. You probabvly also want
>> to set the filename here too
>
> Hmm, yes but I don't want to write two lines of code for each chair I
> create, I want them in
> a for loop, if that is convienient?

Yes, remember the technique of defining the data in a file or data 
structure? Just make the function to launch the chairs take a parameter
of the filename. Then pass the filename in using a lambda in the widget 
definition part of the loop, like this pseudocode:

for n in number_of_destinations:
      filename = make_filename()
      b = Button(parent, label, color,
                 command=lambda f=filename : ChairWindow(f) )

>> I see you are still defining your classes inside your functions. It
>> really is more normal (and useful) to define them all at the top level.
>> It also makes your functions easier to read!
>
> Really? I actually thinks it is easier to read the code using inner
> classes, but thats just me probably!


Its harder to read because you are splitting your code


def FunctionNameHere(args here):
     some code here
     some more
     class Fooo
        lots of class specific stiuff
         more
         more
         etc/...

     back to that method again,
     now where were we?????

The class code just acts to separate the function definition and early 
code fom the main body of code that defines it. And if the class has 
lots of functions its even more confuising and difficult to be sure 
where the outer function resumes.

Plus of course that the inner class is completely inaccessible
outside the function. But one of the greatest benefits of classes is 
that they allow easy reuse - but only if they are visible!

>> class Destination1_Chairs(Frame):
>
> This is the thing you want to call from your destination buttons. So
> create a method in the top level window that creates one of these objects.

So as per my pseudo code above you probvably need to add a filename 
parameter to your init method.


-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/



More information about the Tutor mailing list