MyApp now creating 2 instances of MyFrame

I’m having an unusual problem with my code generating 2 instances of a frame, neither of which are working correctly. The frame can no longer see a class called Repository in another module. This has been working fine for a long time until yesterday when I changed the code in MyApp to replace init with OnInit. I also added the database login stuff which was part of another module. I’ve since changed it back but the problem remains. Here is the code:

import wx
import sys
sys.path.append(’~/PyPrograms/EMRGUI’)
import Selectable
import MySQLdb

class MyApp(wx.App):
def init(self):

    wx.App.__init__(self)
    self.conn = MySQLdb.connect(host = "localhost",    #connects to the database
         user = "someuser",
         passwd = "somepw",

         db = "meds")
    frame = MyFrame(None, -1, 'EMR')                   #create instance of MyFrame
    frame.Show(True)                                   #make visible and center frame

    frame.Centre()

def OnExit(self):
    self.conn.close()                                  #closes connection to MySQL

class MyFrame(wx.Frame):
def init(self, parent, id, title):

    wx.Frame.__init__(self, parent, id, title, size=(900,550))
    self.nb = wx.Notebook(self)                      #create instance of wx.Notebook
    self.page1 = Form1(self.nb, -1)                  #create instance of panel Form1 with Notebook instance as parent

    self.nb.AddPage(self.page1, "Choose Patient")    #add the panels as Notebook pages

def patient_lookup(self, first_ltrs):                          #passes first letters of last name and creates new page c results

    self.page2 = Selectable.Repository(self.nb, -1, first_ltrs)    #creates instance of panel Repository
    self.nb.AddPage(self.page2, "Patient Lookup")              #adds second page with results

    self.page2.SetFocus()                                      #give focus to new page (this isn't working)

class Form1(wx.Panel):
def init(self, parent, id):
wx.Panel.init(self, parent, id) #inherits from wx.Panel
self.button = wx.Button(self, 10, “Search”, wx.Point(225, 100)) #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,65))
    self.editname = wx.TextCtrl(self, 20, "", wx.Point(225, 60), wx.Size(140,-1))
    wx.EVT_BUTTON(self, 10, self.onClick)                              #calls function to get list of patients
    self.editname.SetFocus()

def onClick(self, event):
    f = wx.GetTopLevelParent(self)                                     #gets reference to instance of MyFrame

    f.patient_lookup(self.editname.Value)                              #now calling the MyFrame function works

app = MyApp() #create instance of MyApp

app.MainLoop() #run program

Thanks for any help.

Mike Barron

sys.path.append('~/PyPrograms/EMRGUI')

...

             db = "meds")

...

        self.nb.AddPage(self.page1, "Choose Patient") #add the panels as

...

    def patient_lookup(self, first_ltrs): #passes
first letters of last name and creates new page c results

...

        self.nb.AddPage(self.page2, "Patient Lookup") #adds

...

#calls function to get list of patients

Michael,

any chance code (and database schema) could be reviewed for
inspiration ?

Karsten
http://wiki.gnumed.de

···

On Fri, Nov 30, 2007 at 11:24:06AM -0500, Michael Barron wrote:
--
GPG key ID E4071346 @ wwwkeys.pgp.net
E167 67FD A291 2BEA 73BD 4537 78B9 A9F9 E407 1346

Michael Barron wrote:

I'm having an unusual problem with my code generating 2 instances of a frame, neither of which are working correctly. The frame can no longer see a class called Repository in another module. This has been working fine for a long time until yesterday when I changed the code in MyApp to replace __init__ with OnInit. I also added the database login stuff which was part of another module. I've since changed it back but the problem remains. Here is the code:

[...]

   app = MyApp() #create instance of MyApp
app.MainLoop() #run program

If this module is imported by the other module, and is also run as a script then this code will be executed twice. Try the "if __name__ == '__main__': ..." trick.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

I got it fixed. I think there were a number of things I was doing wrong. Among the different modules I was importing the other modules. This seemed to result in a duplication that caused several instances of my frame to load. I believe this also caused the modules to not import because they were running as “main”.

For a while I couldn’t get things to work with my database login under the MyApp class but I eventually guessed that wx.GetApp() can be used to refer to ‘conn’ from other modules. This way I can open and close the cursor as needed while keeping the connection open.

Thanks for the help.

Mike

···