Tkinter app structure

Richard Lewis richardlewis at
Mon Jun 13 17:45:11 CEST 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
'' 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

I've also created a '' 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 module imports:

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

* The module imports:

from Tkinter import *
from commands import *

* The modules imports:

import main
import tkMessageBox

* And the module imports only unrelated modules

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 looks like this:
1: from dataclasses import *
2: from mainwindow import *
3: from Tkinter import *
5: data = Database()
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 "./", line 2, in ?
    from mainwindow import *
  File "", line 2, in ?
    from commands import *
  File "", line 4, in ?
    import main
  File "", line 8, in ?
    mwnd = MainWindow(root)
NameError: name 'MainWindow' is not defined

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

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


More information about the Python-list mailing list