wxPython - SetLabel 不会在 类 之间传递值时触发

wxPython - SetLabel doesn't trigger on passing values between classes

我正在尝试在 wxPython 中的 类 之间传递值并且似乎在工作(传递的值打印到控制台)但我不知道为什么 bottomLabel 不改变它的值 - 参数传递正确但 changeLabel() 方法不更新标签。

代码如下:

import wx

class TopPanel(wx.Panel):

    def __init__(self, parent):
        Main.btm = BottomPanel(parent=parent)

        wx.Panel.__init__(self, parent=parent)
        self.label = wx.StaticText(self, label='Choose your animal:', pos=(10, 30))
        animals = ["dog", "cat", "mouse"]
        self.combobox = wx.ComboBox(self, choices=animals, pos=(10, 50))
        self.label2 = wx.StaticText(self, label="", pos=(10, 80))
        self.combobox.Bind(wx.EVT_COMBOBOX, self.onCombo)


    def onCombo(self, event):
        comboValue = self.combobox.GetValue()
        self.label2.SetLabel("Chosen animal: " + comboValue)
        Main.animal = comboValue
        Main.btm.changeLabel(event, comboValue)


class BottomPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        self.SetBackgroundColour("grey")
        self.bottomLabel = wx.StaticText(self, label="MyBottomLabel", pos=(10, 50))

    def changeLabel(self, event, passedText):
        self.bottomLabel.SetLabel(passedText)
        print(passedText)

class Main(wx.Frame):
    btm=None
    animal = ""

    def __init__(self):
        wx.Frame.__init__(self, parent=None, title="Generator", size=(500, 500))
        splitter = wx.SplitterWindow(self)
        top = TopPanel(splitter)
        bottom = BottomPanel(splitter)
        splitter.SplitHorizontally(top, bottom)
        splitter.SetMinimumPaneSize(250)

if __name__ == "__main__":
    app = wx.App(False)
    frame = Main()
    frame.Show()
    app.MainLoop()

我在 Python 中对 OOP 很陌生,如果代码不是最优雅的,我深表歉意。

你选择了一个特别痛苦的例子,因为面板的父级不是 Main 而是 splitter 并且你已经将 Main 中的所有内容声明为局部变量,而不是使用self.
这是 a 的快速方法。您会注意到我必须在 top 之前声明 bottom ,否则在声明 TopPanel 时它不存在。我确信有一种更简洁的方法可以做到这一点。

import wx

class TopPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        Main = parent.GetParent()
        self.btm = Main.bottom
        self.label = wx.StaticText(self, label='Choose your animal:', pos=(10, 30))
        animals = ["dog", "cat", "mouse"]
        self.combobox = wx.ComboBox(self, choices=animals, pos=(10, 50))
        self.label2 = wx.StaticText(self, label="", pos=(10, 80))
        self.combobox.Bind(wx.EVT_COMBOBOX, self.onCombo)


    def onCombo(self, event):
        comboValue = self.combobox.GetValue()
        self.label2.SetLabel("Chosen animal: " + comboValue)
        self.btm.changeLabel(event, comboValue)


class BottomPanel(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent=parent)
        self.SetBackgroundColour("grey")
        self.bottomLabel = wx.StaticText(self, label="MyBottomLabel", pos=(10, 50))

    def changeLabel(self, event, passedText):
        self.bottomLabel.SetLabel(passedText)
        print(passedText)

class Main(wx.Frame):

    def __init__(self):
        wx.Frame.__init__(self, parent=None, title="Generator", size=(500, 500))
        self.splitter = wx.SplitterWindow(self)
        self.bottom = BottomPanel(self.splitter)
        self.top = TopPanel(self.splitter)
        self.splitter.SplitHorizontally(self.top, self.bottom)
        self.splitter.SetMinimumPaneSize(250)

if __name__ == "__main__":
    app = wx.App(False)
    frame = Main()
    frame.Show()
    app.MainLoop()