How to pass variables to Main Frame?

Viewed 28

I can pass the variables from Main Frame to Second Frame, however i couldn't succeed on do it vice versa?

The error:

Traceback (most recent call last):
  File "C:\Users\GCS-User\Desktop\with canvas\__or(class_variable).py", line 33, in OnButton2
    self.textCtrl1.SetValue(self.parent.z)
AttributeError: 'NoneType' object has no attribute 'z'

The code:

# -*- coding: utf-8 -*- 

import wx

class A (wx.Frame):
    def __init__(self, parent):
        self.parent = parent
        wx.Frame.__init__(self, parent)
        bSizer1 = wx.BoxSizer( wx.VERTICAL )
        self.startButton1 = wx.Button( self, wx.ID_ANY, u"Button1", wx.DefaultPosition, wx.DefaultSize, 0 )       
        bSizer1.Add( self.startButton1, 1, wx.ALL|wx.EXPAND, 5 )
        self.startButton2 = wx.Button( self, wx.ID_ANY, u"Button2", wx.DefaultPosition, wx.DefaultSize, 0 )       
        bSizer1.Add( self.startButton2, 1, wx.ALL|wx.EXPAND, 5 )

        self.textCtrl1 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.textCtrl1, 1, wx.ALL|wx.EXPAND, 5 )

        self.SetSizer( bSizer1 )
        self.Layout()

        self.startButton1.Bind(wx.EVT_BUTTON, self.OnButton1)
        self.startButton2.Bind(wx.EVT_BUTTON, self.OnButton2)
        self.count = 0


    def OnButton1(self, event):
        self.x = 5 + self.count
        self.y = 7 + self.count
        B(self).Show()
        self.count += 1

    def OnButton2(self, event):
        self.textCtrl1.SetValue(self.parent.z)

class B(wx.Frame):
    def __init__(self, parent):
        self.parent = parent
        wx.Frame.__init__(self, parent)
        bSizer1 = wx.BoxSizer( wx.HORIZONTAL )

        self.startButton3 = wx.Button( self, wx.ID_ANY, u"Button3", wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.startButton3, 1, wx.ALL|wx.EXPAND, 5 )

        self.textCtrl2 = wx.TextCtrl( self, wx.ID_ANY, wx.EmptyString, wx.DefaultPosition, wx.DefaultSize, 0 )
        bSizer1.Add( self.textCtrl2, 1, wx.ALL|wx.EXPAND, 5 )

        self.SetSizer( bSizer1 )
        self.Layout()

        self.startButton3.Bind(wx.EVT_BUTTON, self.OnButton3)

        self.z = self.parent.x + self.parent.y

    def OnButton3(self, event):
        self.textCtrl2.SetValue(str(self.z))
        #self.parent.textCtrl1.SetValue(str(self.z))


app = wx.App()
frame = A(None)
frame.Show(True)
app.MainLoop()

Like second frame have self.parent = parent, I placed it in Main Frame too. Unfortunately it didn't work. How can i pass the variables from second frame to main frame by pressing Button 2?

Any suggestion would be appreciated. Thanks.

1 Answers

The problem is that you are not saving a pointer to frame B. With B(self).Show() you create the frame but there is no easy way to refer to frame B from frame A.

Just modify how you create frame B. See the modified methods of class A below.

def OnButton1(self, event):
    self.x = 5 + self.count
    self.y = 7 + self.count
    self.bFrame = B(self)
    self.bFrame.Show()
    self.count += 1

def OnButton2(self, event):
    self.textCtrl1.SetValue(str(self.bFrame.z))
Related