Manipulation variables in a module's parent

Say I have one wxFrame that creates a child wxFrame. I’m not sure
how to have the child set/modify/read a variable from the parent
frame. I know you need a reference to the parent in the child buy
using some form of passing “self” to the child but so far I’ve not been
able to figure out how to do this. Can someone modify the code
below to show me how to read/set the variable intTest in the parent
FROM the child? (If it matters, I’m using wxGlade to build my
interfaces and would like to keep the basic way of calling/creating the
wxFrames if I can.)

import wx

class ChildFrame(wx.Frame):
def init(self, *args, **kwds):
kwds[“style”] = wx.DEFAULT_FRAME_STYLE
wx.Frame.init(self, *args, **kwds)
self.button_2 = wx.Button(self, -1, “button_2”)
self.SetTitle(“frame2”)
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.button_2, 0, wx.ADJUST_MINSIZE, 0)
self.SetAutoLayout(True)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
sizer_1.SetSizeHints(self)
self.Bind(wx.EVT_BUTTON, self.OnButton, self.button_2)

def OnButton(self, event):
    # I want to modify/read the intTest variable from the parent here
    # **SOMETHING**.intTest = 20
    event.Skip()

class MyFrame(wx.Frame):
intTest = 10 # THIS IS THE VAR I WANT THE CHILD TO MODIFY/READ

def __init__(self, *args, **kwds):
    kwds["style"] = wx.DEFAULT_FRAME_STYLE
    wx.Frame.__init__(self, *args, **kwds)
    self.button_1 = wx.Button(self, -1, "button_1")
    self.SetTitle("frame1")
    sizer_1 = wx.BoxSizer(wx.VERTICAL)
    sizer_1.Add(self.button_1, 0, wx.ADJUST_MINSIZE, 0)
    self.SetAutoLayout(True)
    self.SetSizer(sizer_1)
    sizer_1.Fit(self)
    sizer_1.SetSizeHints(self)
    self.Bind(wx.EVT_BUTTON, self.OnButton, self.button_1)

def OnButton(self, event):
    cframe = ChildFrame(None, -1, "")
    cframe.Show()
    event.Skip()

class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, “”)
self.SetTopWindow(frame_1)
frame_1.Show()
return 1

if name == “main”:
app = MyApp(0)
app.MainLoop()

Hi Ryan,

wx.Frame has a GetParent() method that might do what you want.
def OnButton(self, event):
   # modify/read the intTest variable from the parent here
   self.GetParnet().intTest = 20
   event.Skip()

adi

···

Am Thu, 18 Aug 2005 14:20:57 +0200 schrieb Ryan McDonald <mcdonald.ryan@gmail.com>:

Say I have one wxFrame that creates a child wxFrame. I'm not sure how to
have the child set/modify/read a variable from the parent frame. I know you
need a reference to the parent in the child buy using some form of passing
"self" to the child but so far I've not been able to figure out how to do
this. Can someone modify the code below to show me how to read/set the
variable intTest in the parent FROM the child? (If it matters, I'm using
wxGlade to build my interfaces and would like to keep the basic way of
calling/creating the wxFrames if I can.)

import wx

class ChildFrame(wx.Frame):
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.button_2 = wx.Button(self, -1, "button_2")
self.SetTitle("frame2")
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.button_2, 0, wx.ADJUST_MINSIZE, 0)
self.SetAutoLayout(True)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
sizer_1.SetSizeHints(self)
self.Bind(wx.EVT_BUTTON, self.OnButton, self.button_2)

def OnButton(self, event):
# I want to modify/read the intTest variable from the parent here
# **SOMETHING**.intTest = 20
event.Skip()
class MyFrame(wx.Frame):
intTest = 10 # THIS IS THE VAR I WANT THE CHILD TO MODIFY/READ
def __init__(self, *args, **kwds):
kwds["style"] = wx.DEFAULT_FRAME_STYLE
wx.Frame.__init__(self, *args, **kwds)
self.button_1 = wx.Button(self, -1, "button_1")
self.SetTitle("frame1")
sizer_1 = wx.BoxSizer(wx.VERTICAL)
sizer_1.Add(self.button_1, 0, wx.ADJUST_MINSIZE, 0)
self.SetAutoLayout(True)
self.SetSizer(sizer_1)
sizer_1.Fit(self)
sizer_1.SetSizeHints(self)
self.Bind(wx.EVT_BUTTON, self.OnButton, self.button_1)

def OnButton(self, event):
cframe = ChildFrame(None, -1, "")
cframe.Show()
event.Skip()

class MyApp(wx.App):
def OnInit(self):
wx.InitAllImageHandlers()
frame_1 = MyFrame(None, -1, "")
self.SetTopWindow(frame_1)
frame_1.Show()
return 1

if __name__ == "__main__":
app = MyApp(0)
app.MainLoop()

You should pass the parent (self) to the ChildFrame:
    def OnButton(self, event):
        cframe = ChildFrame(self, -1, "")

and in ChildFrames OnBtn, you can access simply with:

    def OnButton(self, event):
        # I want to modify/read the intTest variable from the parent here
        # **SOMETHING**.intTest = 20
        print self.GetParent().intTest

···

On Thu, 18 Aug 2005 07:20:57 -0500, Ryan McDonald <mcdonald.ryan@gmail.com> wrote:

Say I have one wxFrame that creates a child wxFrame. I'm not sure how to
have the child set/modify/read a variable from the parent frame. I know you
need a reference to the parent in the child buy using some form of passing
"self" to the child but so far I've not been able to figure out how to do
this. Can someone modify the code below to show me how to read/set the
variable intTest in the parent FROM the child? (If it matters, I'm using
wxGlade to build my interfaces and would like to keep the basic way of
calling/creating the wxFrames if I can.)

--
Franz Steinhaeusler

Others beat me to the reply :slight_smile:
What I can add to their solution is this:
Learn about GUI patterns, especially about MVC and its successor MVP.
I personally prefer the PresenterFirst variant of MVP.

http://www.object-arts.com/EducationCentre/Patterns/MVP.htm

http://www.darronschall.com/weblog/archives/000113.cfm
http://atomicobject.com/media/files/PresenterFirst.pdf

Any time invested in learning these patterns will pay back tenfold latter.

Peter.

···

On Thu, 18 Aug 2005 15:20:57 +0300, Ryan McDonald <mcdonald.ryan@gmail.com> wrote:

Say I have one wxFrame that creates a child wxFrame. I'm not sure how to
have the child set/modify/read a variable from the parent frame. I know you
need a reference to the parent in the child buy using some form of passing
"self" to the child but so far I've not been able to figure out how to do
this. Can someone modify the code below to show me how to read/set the
variable intTest in the parent FROM the child? (If it matters, I'm using
wxGlade to build my interfaces and would like to keep the basic way of
calling/creating the wxFrames if I can.)