我无法使用wx事件调用来获取我的变量以在类和选项卡之间进行更新

时间:2012-07-05 23:07:21

标签: python wxpython

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

1 个答案:

答案 0 :(得分: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实例根本没有相互通话,因此panel1无法了解panel2的状态}。您有几种方法可以解决这个问题。最简单的方法是将panel2作为init的{​​{1}}参数传递。像这样:

panel1

这仍然不是一个好的解决方案。一般来说,我尝试使用PubSub来提供一些东西来在类之间传递信息而不会产生依赖性(即注意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 如何PanelOne永远不会存在PanelTwo,即使是在原始版本中也是如此代码),但我认为这对你的目的来说过于复杂。