how do I pass values between classes?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kath

    #1

    how do I pass values between classes?

    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 OR 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()
    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()
    thank you,
    regards,
    kath.

  • MC

    #2
    Re: how do I pass values between classes?

    Hi!

    Look Candygram (http://candygram.sourceforge.net/)
    Candygram has features who will fun for you (like
    inter-threads-comunication).

    --
    @-salutations

    Michel Claveau


    Comment

    • Larry Bates

      #3
      Re: how do I pass values between classes?

      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 OR 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()
      >
      >
      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()
      >
      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

      Comment

      • kath

        #4
        Re: how do I pass values between classes?

        hi, Larry Bates .... thanks for the reply...
        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.
        Yes thats absolutely right..
        That way the subclass can
        easily pass values back to the parent by using that pointer.
        Could you please explain me this.. more clearly. I think it is much
        close to the solution.


        Thank you.
        regards, sudhir

        Comment

        • Max Erickson

          #5
          Re: how do I pass values between classes?

          "kath" <nitte.sudhir@g mail.comwrote:
          hi, Larry Bates .... thanks for the reply...
          >
          >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.
          >
          Yes thats absolutely right..
          >
          >That way the subclass can
          >easily pass values back to the parent by using that pointer.
          >
          Could you please explain me this.. more clearly. I think it is much
          close to the solution.
          >
          >
          Thank you.
          regards, sudhir
          >
          Somewhere in the child class:

          self.parent.do_ something(somet hing_to_do_it_w ith)

          Comment

          • jim-on-linux

            #6
            Re: how do I pass values between classes?


            Kath,
            You can use this class to pass values around
            without concern for conflicts since it has no
            values of its own.


            class Kvariable:
            def setVariable(sel f, variable):
            self.output = variable

            def showVariable(se lf):
            print self.output

            x = Kvariable()
            y = Kvariable()

            x.setVariable(" James_01")
            y.setVariable(" Kath_01")

            x.showVariable( )
            y.showVariable( )


            x.setVariable(' 3.14159')
            y.setVariable(" python.org")

            x.showVariable( )
            y.showVariable( )

            jim-on-linux

            http://www.inqvista.com














            On Monday 06 November 2006 02:00, kath wrote:
            hi, Larry Bates .... thanks for the reply...
            >
            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.
            >
            Yes thats absolutely right..
            >
            That way the subclass can
            easily pass values back to the parent by
            using that pointer.
            >
            Could you please explain me this.. more
            clearly. I think it is much close to the
            solution.
            >
            >
            Thank you.
            regards, sudhir

            Comment

            • Larry Bates

              #7
              Re: how do I pass values between classes?

              kath wrote:
              hi, Larry Bates .... thanks for the reply...
              >
              >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.
              >
              Yes thats absolutely right..
              >
              >That way the subclass can
              >easily pass values back to the parent by using that pointer.
              >
              Could you please explain me this.. more clearly. I think it is much
              close to the solution.
              >
              >
              Thank you.
              regards, sudhir
              >
              Just something like:

              class foo:
              self.__init__(s elf, parent):
              self.parent=par ent

              class bar:
              self.__init__(s elf, parent):
              self.parent=par ent
              self.foo=foo(se lf)


              class myclass:
              self.__init__(s elf, parent):
              self.parent=par ent
              self.bar=bar(se lf)

              Now in foo I can reference things from myclass easily
              self.parent.par ent. Or setattr(self.pa rent.parent, value).

              Hope this helps.

              -Larry

              Comment

              Working...