Importing modules

jmdeschamps at cvm.qc.ca jmdeschamps at cvm.qc.ca
Sat May 14 08:10:12 EDT 2005


CONTEXT:
Trying to modularize (is this english?) application development : MVC
style, team distributed so that a visual component can be in one module
imported and used  by the __main__, and connected (read packed) at
runtime to other components coming from the main or other modules.

PROBLEM:
After reading the above reference (and the Nutshell chapter) and trying
different importing variations I still have a very difficult time
understanding this situation.
1-importing Tkinter in main,
2-importing module that uses Tkinter, without importing Tkinter in that
module (DOES NOT WORK)
KLUDGED SOLUTION:
import Tkinter in module (but don't understand why it has to be this
way)

MODEL (I taught would work):
## importTestMain module
from Tkinter import *
import testModule

tata = testModule.toto() ## this works
myWidget = tata.createNewFrame() ## this gets the exception (see below)
...

## testModule
class toto(object):
    def __init__(self):
        self.someAttribute= 2
    def createNewFrame(self):
        self.myFrame = Frame()    ## culprit line in exception
        return f

if __main__=='__name__':  ## team member tests is work here (does
work!)
    from Tkinter import *
    myInstance = otherModule.toto()
    myWidget = myInstance.createNewFrame()
    root=Tk()
    myWidget.pack(in_=root)
    root.mainloop()

The exception says :
Traceback (most recent call last):
  File
"C:\Python23\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py",
line 310, in RunScript
    exec codeObject in __main__.__dict__
  File
"C:\jean_marc\exemples_code_source\modules_packages\import_test\importTestMain.py",
line 18, in ?
    tata=toto()
  File
"C:\jean_marc\exemples_code_source\modules_packages\import_test\importTestMain.py",
line 10, in __init__
    self.myNewFrame =
self.myTestModuleObject.createNewFrame(self.myInterModuleTestCommand)
  File
"C:\jean_marc\exemples_code_source\modules_packages\import_test\testModule.py",
line 8, in createNewFrame
    self.myFrame = Frame()
NameError: global name 'Frame' is not defined
>>>

VARIATION
Imported testModule AFTER using Tkinter in main... same error :(




More information about the Python-list mailing list