Reputation: 1
If you run this code, you will get default values from a combo box on Panel One, but after changing the values on Panel 2, the values being called from the Panel 1 button do not reflect the change in values on Panel 2.
import wx
class PanelOne(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
self.button1 = wx.Button(self, -1, label="Panel 1 Test",
pos=wx.Point(100, 100))
self.button1.Bind(wx.EVT_BUTTON, self.button1Click, self.button1)
def button1Click(self,event):
p2=PanelTwo(self, 0)
print "Panel 1: %s" % p2.C1
print "Panel 1: %s" % p2.C2
class PanelTwo(wx.Panel):
def __init__(self, parent, id):
wx.Panel.__init__(self, parent, id)
# Default Declarations
self.C1 = List1[0]
self.C2 = List2[-1]
self.combo1 = wx.ComboBox(self, -1, value=List1[0], pos=wx.Point(100, 30),
size=wx.Size(100, 150), choices=List1, style = wx.CB_READONLY)
self.Bind(wx.EVT_COMBOBOX, self.EvtCmb1, self.combo1)
self.combo2 = wx.ComboBox(self, -1, value=List2[-1], pos=wx.Point(300, 30),
size=wx.Size(100, 150), choices=List2, style = wx.CB_READONLY)
self.Bind(wx.EVT_COMBOBOX, self.EvtCmb2, self.combo2)
self.button1 = wx.Button(self, -1, label="Panel 2 Test",
pos=wx.Point(100, 100))
self.button1.Bind(wx.EVT_BUTTON, self.button1Click, self.button1)
def button1Click(self,event):
print "Panel 2: %s" % self.C1
print "Panel 2: %s" % self.C2
def EvtCmb1(self, event):
combo1 = (event.GetString())
self.C1 = combo1
def EvtCmb2(self, event):
combo2 = (event.GetString())
self.C2 = combo2
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Testing 123", size = (500, 400) )
p = wx.Panel(self)
nb = wx.Notebook(p)
panel1 = PanelOne(nb,-1)
panel2 = PanelTwo(nb,-1)
nb.AddPage(panel1, "Page One")
nb.AddPage(panel2, "Page Two")
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
#Combo1
List1 = ['Apple', 'Banana']
#Combo2
List2 = ["1", "2", "3", "4"]
app = wx.App(redirect=False)
frame = MainFrame()
frame.Show(True)
app.MainLoop()
Upvotes: 0
Views: 205
Reputation: 3113
I find your code confusing, but I think I found the problem. Consider your PanelOne.button1Click()
function:
def button1Click(self,event):
p2=PanelTwo(self, 0)
print "Panel 1: %s" % p2.C1
print "Panel 1: %s" % p2.C2
You're creating a new instance of PanelTwo
every time, meaning it will always have the default values. Think of classes like makes and models of cars. I can have a Toyota Camry and you can have a Toyota Camry. They are the same type (class) of car, but they are seperate cars (different instances). Any change I make to my Camry obviously won't affect yours since they are different objects entirely. So following this analogy, everytime PanelOne.button1Click()
is called, it buys a new Camry (creates a new instance); it doesn't get an already existing one. That new car comes fresh from the factory with all the default options regardless of how other owners modify their cars.
So basically, your panel1
instance and your panel2
instance aren't talking to each other at all, so panel1
can't know anything about the state of panel2
. You have a few ways you can solve this. The easiest way is to pass panel2
as an init
argument for panel1
. Like this:
class PanelOne(wx.Panel):
def __init__(self, parent, id, p2):
wx.Panel.__init__(self, parent, id)
self.p2 = p2 #store panel2 so we can check it later
#do the rest of your setup
def button1Click(self,event):
print "Panel 1: %s" % self.p2.C1
print "Panel 1: %s" % self.p2.C2
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="Testing 123", size = (500, 400) )
panel2 = PanelTwo(nb,-1)
panel1 = PanelOne(nb,-1, panel2) #panel1 needs to be able to read panel2's settings
#do the rest of your setup
This still isn't a great solution. Generally, I'd try to come up with something using PubSub to pass the information between classes without causing a dependency (i.e. note how PanelOne
can never exist wthout PanelTwo
, even in your original code), but I think that's overly complicated for your purposes.
Upvotes: 1