0

如果运行此代码,您将从面板 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()
4

1 回答 1

1

我发现您的代码令人困惑,但我认为我发现了问题。考虑你的PanelOne.button1Click()功能:

def button1Click(self,event):
    p2=PanelTwo(self, 0)
    print "Panel 1: %s" % p2.C1
    print "Panel 1: %s" % p2.C2

PanelTwo每次都在创建一个新实例,这意味着它将始终具有默认值。想想汽车的品牌和型号等类。我可以有一辆丰田凯美瑞,你可以有一辆丰田凯美瑞。它们是同一类型(类别)的汽车,但它们是不同的汽车(不同的实例)。我对我的凯美瑞所做的任何改变显然都不会影响的,因为它们完全是不同的对象。所以按照这个类比,每次PanelOne.button1Click()调用,它都会购买一个新的凯美瑞(创建一个新的实例);它没有一个已经存在的。无论其他车主如何改装他们的汽车,这辆新车都带有所有默认选项。

所以基本上,你的panel1实例和你的panel2实例根本不互相交谈,所以panel1panel2. 你有几种方法可以解决这个问题。最简单的方法是panel2作为. 像这样:initpanel1

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

这仍然不是一个很好的解决方案。通常,我会尝试使用PubSub在类之间传递信息而不会产生依赖关系(即注意即使在您的原始代码中PanelOne也不可能存在PanelTwo),但我认为这对于您的目的来说过于复杂。

于 2012-07-06T03:42:25.627 回答