[Tutor] Sharing Variables Across Modules
Andre Roberge
andre.roberge at gmail.com
Tue Feb 7 03:24:17 CET 2006
On 2/6/06, Rich Shepard <rshepard at appl-ecosys.com> 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'
I would *guess* that you'd want instead something like
tcName = self.myNotebook.modModel.tcName
As I understand, modModel is a page in a wx.Notebook; you probably
created it within your Notebook instance, and not your Frame instance.
Meanwhile, your OnFileNew method appears to belong to your Frame
instance.
André
>
> How do I make tcName visible in eikos.py when it's defined in modelPage.py?
>
> Rich
>
> --
> Richard B. Shepard, Ph.D. | Author of "Quantifying Environmental
> Applied Ecosystem Services, Inc. (TM) | Impact Assessments Using Fuzzy Logic"
> <http://www.appl-ecosys.com> Voice: 503-667-4517 Fax: 503-667-8863
>
>
>
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list