如果运行此代码,您将从面板 1 上的组合框中获取默认值,但在更改面板 2 上的值后,从面板 1 按钮调用的值不会反映面板 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()