Classes

Skip Montanaro skip at pobox.com
Mon Nov 11 08:19:05 EST 2002


    newt> I'm putting together an application that uses Tkinter for the
    newt> GUI. At the moment I'm using separate classes for each of the
    newt> screens I want to use. Is this the usual way of doing it?

Yes.

    newt> I've also added a __init__ to each class, so that it can call a
    newt> do_widgets routine (in the class) which creates and places all the
    newt> widgets. Is this the ususal way?

More-or-less.  Creating and placing the widgets is frequently done right in
the __init__ method.

    newt> If I wanted to pass some values into my class how do I change the
    newt> __init__ to accept the input? [I suppose part of the problem is
    newt> because I'm calling the class with the command ClassName(self) ]

No "self" is needed.  To create a class which has three explicit parameters,
define it as

    class ClassName:
        def __init__(self, one, two, three):
            ...

then instantiate it like so:

    instance = ClassName(a,b,c)

    newt> Is the correct way to do this: in the class module, get the
    newt> __init__ to do not very much, and then call the do_widgets
    newt> routine?

In all depends.  How complex are your screens?  How dynamic are they (do
widgets come and go)?  What initialization is repeated each time a screen is
displayed?

-- 
Skip Montanaro - skip at pobox.com
http://www.mojam.com/
http://www.musi-cal.com/




More information about the Python-list mailing list