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