how do I pass values between classes?

Larry Bates larry.bates at websafe.com
Sat Nov 4 09:08:15 EST 2006


kath wrote:
> hi everyone.........
> 
> I have a task, I have fragmented the task into subtask. I have planned
> to create a class to each subclass and one parent class to handle the
> sub tasks. Each subclass are saved in seperate file and all my
> subclasses and parent class are placed in the same folder.
> 
> The parent class is subclass of wx.Frame, and subclasses are subclass
> of wx.Panel. Each subclass will create a GUI for each task..
> 
> ok.. here is problem
> 
> When a menu is selected in the parent class, im instantiating the child
> class(i.e, Panel), which has textbox and two buttons. first button is
> used to select a file, and second one I have planned to, read the file
> and pass the values to parent class [B]OR[/B] pass the filepath to
> parent to read and do rest of the work.
> 
> How do I do that?
> 
> Please tell me whether I am following a correct method, by fragmenting
> task  into small task and assigning it to child class and saving it in
> a separate file?.
> 
> ok, here is the Parent class
> [code]
> import wx
> import client
> """My Parent class"""
> class Parent(wx.Frame):
>     def __init__(self, parent, id, title):
>         wx.Frame.__init__(self, parent, id, title)
> 
>         menu=wx.Menu()
>         menu.Append(5000, "File")
>         menu.AppendSeparator()
>         menu.Append(5001, "Exit")
>         menubar=wx.MenuBar()
>         menubar.Append(menu, "File")
>         self.SetMenuBar(menubar)
>         self.Bind(wx.EVT_MENU, self.OnFile,id=5000)
>         self.Bind(wx.EVT_MENU, self.OnExit,id=5001)
> 
>     def OnFile(self, event):
>         self.cl=client.Child(self, -1)
> 
>     def OnBrowse(self, event):
>         dir=""
>         d=x.FileDialog(self, "Choose an Excel file", self.dirname, "",
> "*.xls", wx.OPEN)
>         if d.ShowModal() == wx.ID_OK:
>             self.filename=d.GetFilename()
>             self.dirname=d.GetDirectory()
>             self.filepath.SetValue(os.path.join(self.dirname,
> self.filename))
> 
>     def OnUpload(self, event):
>         pass
> 
>     def OnExit(self, event):
>         self.Close()
> 
> class MyApp(wx.App):
>     def OnInit(self):
>         frame=Parent(None, -1, "Parent window")
>         frame.Show(True)
>         self.SetTopWindow(frame)
>         return True
> 
> if __name__ == '__main__':
>     app=MyApp(redirect=False)
>     app.MainLoop()
> 
> [/code]
> 
> and Child class
> [code]
> import wx, os
> 
> """My Child class"""
> 
> class Child(wx.Panel):
>     def __init__(self, parent, id):
>         wx.Panel.__init__(self, parent, id, size=(300,300))
>         box=wx.BoxSizer(wx.HORIZONTAL)
>         label=wx.StaticText(self, -1, "File Path:  ")
> 
>         self.text=wx.TextCtrl(self, -1)
>         browse=wx.Button(self, -1,"Browse")
>         upload=wx.Button(self, -1, "Upload")
>         self.Bind(wx.EVT_BUTTON, self.OnBrowse, browse)
>         self.Bind(wx.EVT_BUTTON, self.OnUpload, upload)
>         box.Add(label)
>         box.Add(self.text)
>         box.Add(browse)
> 
>         mainbox=wx.BoxSizer(wx.VERTICAL)
>         mainbox.Add(box)
>         mainbox.Add(upload)
> 
>         self.SetSizer(mainbox)
>         mainbox.Layout()
>     def OnBrowse(self, event):
>         self.dir=""
>         d=wx.FileDialog(self, "Choose an Excel file", self.dir, "",
> "*.xls", wx.OPEN)
>         if d.ShowModal() == wx.ID_OK:
>             self.filename=d.GetFilename()
>             self.dir=d.GetDirectory()
>             self.text.SetValue(os.path.join(self.dir, self.filename))
> 
>     def OnUpload(self, event):
>         pass
> 
> class MyApp(wx.App):
>     def OnInit(self):
>         frame=wx.Frame(None, -1, "Child window")
>         panel=Child(frame, -1)
>         frame.Show(True)
>         self.SetTopWindow(frame)
>         return True
> 
> if __name__ == '__main__':
>     app=MyApp()
>     app.MainLoop()
> [/code]
> 
> thank you,
> regards,
> kath.
> 
You might consider doing it the same way wx passes things around.
When you instantiate the subclass pass the parent class' instance
as first argument to __init__ method.  That way the subclass can
easily pass values back to the parent by using that pointer.

-Larry



More information about the Python-list mailing list