wx Python - SetLabel не срабатывает при передаче значений между классами - PullRequest
0 голосов
/ 14 июля 2020

Я пытаюсь передать значения между классами в wx Python и, похоже, работает (переданное значение выводится на консоль), но я не знаю, почему 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()

Я новичок в OOP в Python, извините, если код не самый элегантный.

1 Ответ

1 голос
/ 14 июля 2020

Вы выбрали особенно мучительный пример, поскольку родительский элемент для панелей - это не Main, а splitter, и вы объявили все в Main как локальные переменные, не используя self. Вот a быстрый способ сделать это. Вы заметите, что я должен был объявить bottom перед top, иначе он не существует, когда объявлен 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()

введите описание изображения здесь

...