Namespace troubles

B

barronmo

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
 
A

Alan

class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600,500))

nb = wx.Notebook(self)
self.page1 = Form1(nb, -1)
nb.AddPage(self.page1, "Choose Patient")
self.page1.SetFocus()

def patient_lookup(self, first_ltrs):
self.page2 = Selectable.Repository(nb, -1, first_ltrs)
nb.AddPage(self.page2, "Patient Lookup")
self.page2.SetFocus()
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

nb is a local variable within the __init__() method only.

This is the same as, e.g.:

def foo():
i = 0

def bar():
print i

The use of i in bar() is an error, because it is only assigned in
foo()


If you want to make it available to all methods within a class then
you need to attach it to the instance of that class, e.g.:

class MyFrame(wx.Frame):
def __init__(self, parent, id, title):
wx.Frame.__init__(self, parent, id, title, size=(600,500))
self.nb = wx.Notebook(self)
self.page1 = Form1(nb, -1)
self.nb.AddPage(self.page1, "Choose Patient")
self.page1.SetFocus()

def patient_lookup(self, first_ltrs):
self.page2 = Selectable.Repository(nb, -1, first_ltrs)
self.nb.AddPage(self.page2, "Patient Lookup")
self.page2.SetFocus()
 
K

kyosohma

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
 
B

barronmo

Mike and Alan, thanks very much, working fine now. I'll review the
classes references too.

Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top