Tkinter app structure

Christopher Subich majromax at
Mon Jun 13 20:55:31 CEST 2005

Richard Lewis wrote:
> Hi there,
> I've just started my first project with Tkinter.

> This set up should allow me to be able to access and main.root
> from the module and the command callback functions (defined
> in from
> Whats going wrong? Is it a 'circular import'? Is there a better way that
> I could organise these modules?

Yes, this looks like a circular import to me.

Remember, though, that an import statement in Python isn't completely
analagous to a #include statement in C; because of dynamic typing and
the general guideline of duckitude ("if it walks like a duck and quacks
like a duck..."), you usually don't /need/ to have circular imports.

With the presumption that data = Database() is a mutable object, in
that you interact with it normally and don't reassign it anywhere in
your program, why not just pass data as a parameter to
MainWindow.__init__?  Likewise, MainWindow can call commands with
root/data as parameters -- you could even organize your commands into
an instantiated class, and have the CommandClass take both of these as
__init__ arguments.  In the program (MUD Client) that I'm working on, I
have a vaguely similar dependency between interface-agnostic backend
code and the in-development TKinter interface; by wrapping everything
except main() and a few constants inside instantiated classes, I think
I've mostly avoided circular imports.

If you really, truly need data to be a global variable, you could
possibly put it inside its own module, and instantiate it via = Database() from main.  But that's really ugly, and
I'm not 100% sure that it'd work -- I've never had need to try it, and
am writing this off-the-cuff.

But take this advice all with a lagish grain of iodized NaCl, since I'm
a rank newbie at Python.

More information about the Python-list mailing list