I'm trying to pass values between classes in wxPython and seems to be working (the value passed prints to the console) but I don't know why bottomLabel
doesn't change its value - the arguments are passed properly but the changeLabel()
method doesn't update label.
Here's the code:
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()
I'm quite new to OOP in Python so sorry if the code isn't the most elegant.
You have picked a particularly tortured example, as the parent to panels is not Main
but the splitter
and you have declared everything in Main
as local variables, by not using self
.
Here is a
quick way to do it. You'll notice I had to declare bottom
before top
otherwise it doesn't exist when TopPanel
is declared. I'm sure there's a cleaner way to do this.
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()