Tkinter app structure

Richard Lewis richardlewis at fastmail.co.uk
Mon Jun 13 11:45:11 EDT 2005


Hi there,

I've just started my first project with Tkinter.

I've already coded all the data handling classes and have a nice
interface to work with (it happens to be a wrapper around the DOM of a
large(ish) XML document but thats probably not important ;-)

I'm new to Tkinter so I've started in a fairly random place: the main
menu. I noticed that the event mechanism was based on callbacks so, in a
flash of inspiration, I decided to create a new module called
'commands.py' which would contain all my callback methods (meaning I
could use them for a toolbar as well, if I ever add one) and which acts
as an intermediary between the Tkinter classes and my data hanlding
classes.

I've also created a 'main.py' module which holds the Tk object (called
'root'), the top level data object (called 'data') and the main UI
object (called 'mwnd', an instance of a class inherited from Frame
called 'MainWindow').

* The main.py module imports:

from dataclasses import *
from mainwindow import *
from Tkinter import *

* The mainwindow.py module imports:

from Tkinter import *
from commands import *

* The commands.py modules imports:

import main
import tkMessageBox

* And the dataclasses.py module imports only unrelated modules

This set up should allow me to be able to access main.data and main.root
from the commands.py module and the command callback functions (defined
in commands.py) from mainwindow.py.

main.py looks like this:
====================================
1: from dataclasses import *
2: from mainwindow import *
3: from Tkinter import *
4: 
5: data = Database()
6: 
7: root = Tk()
8: mwnd = MainWindow(root)
9: root.mainloop()
====================================

However, when I execute it I get the following error:

Traceback (most recent call last):
  File "./main.py", line 2, in ?
    from mainwindow import *
  File "mainwindow.py", line 2, in ?
    from commands import *
  File "commands.py", line 4, in ?
    import main
  File "main.py", line 8, in ?
    mwnd = MainWindow(root)
NameError: name 'MainWindow' is not defined

(where 'mainwindow.py' line 2 is "from commands import *", commands.py
line 4 is "import main" and the name "MainWindow" is the name of the
class defined in the mainwindow.py module)

Whats going wrong? Is it a 'circular import'? Is there a better way that
I could organise these modules?

Cheers,
Richard



More information about the Python-list mailing list