[Tutor] Sharing Variables Across Modules

Kent Johnson kent37 at tds.net
Tue Feb 7 03:45:21 CET 2006


Rich Shepard wrote:
>    I'm developing a wxPython-based application. The UI is a notebook, with the
> contents of each page in a separate module. In one module (modelPage.py), I
> have defined the class modModel, which is an instance of a wx.Panel class.
> Within modModel is a text control widget named tcName.
> 
>    The main module, eikos.py, imports modModel from modPage.py. The main
> module has two classes: MyNotebook (subclassed from wx.Notebook) and MyFrame,
> which contains the notebook and menus.
> 
>    The File->New menu calls the method OnFileNew. This displays a directory
> dialog so the user can select the directory in which the new file is to be
> placed. Then it displays a text entry dialog for the name of the new file.
> 
>    I am able to enter the new file name and it is correctly assigned to the
> variable, projname. What I have not been able to do is have that same
> filename displayed in the text control widget, tcName. Here is OnFileNew from
> the main module:
> 
>    def OnFileNew(self, event):
>      """ Create a new file and open it."""
>      self.dirname = ''
>      wd = wx.DirDialog(self, "Chose the default directory", ".",
> style=wx.DD_NEW_DIR_BUTTON)
>      if wd.ShowModal() == wx.ID_OK:
>        dirname = wd.GetPath()
>        ted = wx.TextEntryDialog(self, "Enter the project name", "New Project Name", "",
>                                 style=wx.RAISED_BORDER|wx.OK|wx.CANCEL)
>        if ted.ShowModal() == wx.ID_OK:
>          projname = ted.GetValue()
>          tcName = self.modModel.tcName
>          tcName.WriteText(self, projname)
>          ted.Destroy()
>        wd.Destroy()
> 
>    While projname has the correct string, tcName remains undefined:
> 
>    File "eikos.py", line 272, in OnFileNew
>      tcName = self.modModel.tcName
> AttributeError: 'MyFrame' object has no attribute 'modModel'
> 
>    How do I make tcName visible in eikos.py when it's defined in modelPage.py?

My guess is that you have some confusion between module, class and 
instance scope in modelPage.py. It might help to see the code that 
defines self.modModel and the code in modelPage.py that defines tcName.

Kent



More information about the Tutor mailing list