Вы очень близки!
Несколько разумных вариантов использования self
и parent
решают ваши проблемы.
import wx
class Main1 (wx.Frame):
def __init__(self,parent):
wx.Frame.__init__ ( self, parent )
self.bSizer1 = wx.BoxSizer( wx.VERTICAL )
self.Btn1 = wx.Button( self, wx.ID_ANY, u"Btn1", wx.DefaultPosition, wx.DefaultSize, 0 )
self.bSizer1.Add( self.Btn1, 1, wx.ALL|wx.EXPAND, 5 )
self.SetSizer( self.bSizer1 )
self.Btn1.Bind( wx.EVT_BUTTON, self.btn1_f )
def btn1_f( self, event ):
print("here 1")
self.x = 12
self.y = 30
a=Other_a(parent=self).Show()
b=Other_b(parent=self).Show()
class Other_a (wx.Frame):
def __init__(self, parent):
wx.Frame.__init__ (self,parent)
self.parent = parent
self.bSizer2 = wx.BoxSizer( wx.VERTICAL )
self.Btn2 = wx.Button( self, wx.ID_ANY, u"Btn A 2", wx.DefaultPosition, wx.DefaultSize, 0 )
self.bSizer2.Add( self.Btn2, 0, wx.ALL|wx.EXPAND, 5 )
self.SetSizer( self.bSizer2 )
self.Btn2.Bind( wx.EVT_BUTTON, self.btn2_f )
print("here 2")
print(self.parent.x)
def btn2_f(self,foo):
print("here 3")
print(self.parent.y)
class Other_b (wx.Frame):
def __init__(self,parent):
wx.Frame.__init__ ( self, parent )
self.parent = parent
self.bSizer3 = wx.BoxSizer( wx.VERTICAL )
self.Btn3 = wx.Button( self, wx.ID_ANY, u"Btn B 2", wx.DefaultPosition, wx.DefaultSize, 0 )
self.bSizer3.Add( self.Btn3, 0, wx.ALL|wx.EXPAND, 5 )
self.SetSizer( self.bSizer3 )
self.Btn3.Bind( wx.EVT_BUTTON, self.btn3_f )
print("here 4")
def btn3_f(self,event):
print("here 5")
print(self.parent.x)
if __name__ == "__main__":
app = wx.App(False)
frame = Main1(None)
frame.Show(True)
app.MainLoop()
Дает:
python 20190529.py
here 1
here 2
12
here 4
here 3
30
here 5
12
Вотdiff
между вашим кодом и кодом выше:
< a=Other_a(self).Show()
< b=Other_b(self).Show()
---
> a=Other_a(parent=self).Show()
> b=Other_b(parent=self).Show()
23,24c23,25
< def __init__(self, foo):
< wx.Frame.__init__ ( self )
---
> def __init__(self, parent):
> wx.Frame.__init__ (self,parent)
> self.parent = parent
26c27
< self.Btn2 = wx.Button( self, wx.ID_ANY, u"Btn 2", wx.DefaultPosition, wx.DefaultSize, 0 )
---
> self.Btn2 = wx.Button( self, wx.ID_ANY, u"Btn A 2", wx.DefaultPosition, wx.DefaultSize, 0 )
32c33
< print(foo.x)
---
> print(self.parent.x)
36c37
< print(foo.y)
---
> print(self.parent.y)
40a42
> self.parent = parent
42c44
< self.Btn3 = wx.Button( self, wx.ID_ANY, u"Btn 2", wx.DefaultPosition, wx.DefaultSize, 0 )
---
> self.Btn3 = wx.Button( self, wx.ID_ANY, u"Btn B 2", wx.DefaultPosition, wx.DefaultSize, 0 )
50c52
< print(self.x)
---
> print(self.parent.x)