how do I pass values between classes?

K

kath

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.
 
L

Larry Bates

kath said:
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
 
K

kath

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
 
M

Max Erickson

kath said:
hi, Larry Bates .... thanks for the reply...


Yes thats absolutely right..


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(something_to_do_it_with)
 
J

jim-on-linux

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(self, variable):
self.output = variable

def showVariable(self):
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
 
L

Larry Bates

kath said:
hi, Larry Bates .... thanks for the reply...


Yes thats absolutely right..


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__(self, parent):
self.parent=parent

class bar:
self.__init__(self, parent):
self.parent=parent
self.foo=foo(self)


class myclass:
self.__init__(self, parent):
self.parent=parent
self.bar=bar(self)

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

Hope this helps.

-Larry
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top