Namespace troubles
kyosohma at gmail.com
kyosohma at gmail.com
Fri Nov 16 16:16:39 EST 2007
On Nov 16, 2:51 pm, barronmo <barro... at gmail.com> wrote:
> I'm new to programming and new to Python; namespace issues are getting
> the best of me. Can someone help me with the following:
>
> import wx
> import sys
> sys.path.append('~/PyPrograms/EMRGUI')
> import Selectable
>
> class MyApp(wx.App):
> def __init__(self):
> wx.App.__init__(self)
> frame = MyFrame(None, -1, 'EMR') #create instance of MyFrame
> frame.Show(True) #make visible and center frame
> frame.Centre()
>
> class MyFrame(wx.Frame):
> def __init__(self, parent, id, title):
> wx.Frame.__init__(self, parent, id, title, size=(600,500))
>
> nb = wx.Notebook(self) #create instance of wx.Notebook
> self.page1 = Form1(nb, -1) #create instance of panel Form1 with
> Notebook instance as parent
> nb.AddPage(self.page1, "Choose Patient") #add the panels as Notebook
> pages
> self.page1.SetFocus() #give focus to page 1
>
> def patient_lookup(self, first_ltrs): #passes first letters of
> last name and creates new page c results
> self.page2 = Selectable.Repository(nb, -1, first_ltrs) #creates
> instance of panel Repository from Selectable mod
> nb.AddPage(self.page2, "Patient Lookup") #adds second page with
> results
> self.page2.SetFocus() #give focus to new page
>
> class Form1(wx.Panel):
> def __init__(self, parent, id):
> wx.Panel.__init__(self, parent, id) #inherits from wx.Panel
> f = wx.GetTopLevelParent(self)
> self.button = wx.Button(self, 10, "Search", wx.Point(200,
> 325)) #create instance of wx.Button
> wx.Button.SetTransparent(self.button, 100) #experiment with
> SetTransparent method
> self.lblname = wx.StaticText(self, -1, "Enter first letters of
> last name:",wx.Point(20,60))
> self.editname = wx.TextCtrl(self, 20, "", wx.Point(150, 60),
> wx.Size(140,-1))
> wx.EVT_BUTTON(self, 10, f.patient_lookup(self.editname.Value))
> #calls function to get list of patients
>
> app = MyApp() #create instance of MyApp
> app.MainLoop() #run program
>
> I'm getting an error from the patient_lookup function: "global name
> 'nb' is not defined". I don't understand how a function in the same
> class cannot see the wx.Notebook instance "nb". I know it exists
> somewhere I just haven't found the name for it. I've tried
> "frame.nb", "self.nb", f = GetTopLevelParent(self) then f.nb. How can
> an instance of something just disappear?
>
> Thanks for any help.
>
> Mike
If you want to able to access variables or objects from functions (or
methods) within a class, you'll have to put "self" on the front part
of their name. In this case, change nb to self.nb
This makes nb into an attribute of the class that can be accessed by
any method/function within that class.
See the following links for more on classes:
http://docs.python.org/tut/node11.html
http://www.diveintopython.org/object_oriented_framework/defining_classes.html
Mike
More information about the Python-list
mailing list