在SplitterWindow wxpython中将文本从面板更改为其他面板

时间:2018-10-09 02:02:40

标签: wxpython

我如何才能获得这种和平的代码,我的意思是我尝试通过单击按钮来更改文本,这给我一个错误,请帮忙。 谢谢大家。

导入wx

将wx.grid导入为gridlib

RegularPanel类(wx.Panel):

def __init__(self, parent):

    wx.Panel.__init__(self, parent)

    self.SetBackgroundColour("blue")

    button1 = wx.Button(self, label="change the text")

    self.Bind(wx.EVT_BUTTON, self.OnChange, id=button1.GetId())

def OnChange(self, event):

    value = str(self.text.GetLabel())

    value = "this works"

    self.text.SetLabel(str(value))

OtherPanel类(wx.Panel):

def __init__(self, parent):

    wx.Panel.__init__(self, parent)

    self.SetBackgroundColour("white")

    self.text = wx.StaticText(self, -1, 'try to change this text', (40, 60))

GridPanel类(wx.Panel):

def __init__(self, parent):

    wx.Panel.__init__(self, parent)

    self.grid = gridlib.Grid(self, style=wx.BORDER_SUNKEN)

    self.grid.CreateGrid(20,8)

    sizer = wx.BoxSizer(wx.VERTICAL)

    sizer.Add(self.grid, 1, wx.EXPAND)

    self.SetSizer(sizer)

MainPanel类(wx.Panel):

def __init__(self, parent):

    wx.Panel.__init__(self, parent)

    notebook = wx.Notebook(self)

    page = wx.SplitterWindow(notebook)

    notebook.AddPage(page, "Splitter")

    hSplitter = wx.SplitterWindow(page)

    panelOne = OtherPanel(hSplitter)

    panelTwo = GridPanel(hSplitter)

    hSplitter.SplitVertically(panelOne, panelTwo)

    hSplitter.SetSashGravity(0.5)

    panelThree = RegularPanel(page)

    page.SplitHorizontally(hSplitter, panelThree)

    page.SetSashGravity(0.5)

    sizer = wx.BoxSizer(wx.VERTICAL)

    sizer.Add(notebook, 1, wx.EXPAND)

    self.SetSizer(sizer)

MainFrame(wx.Frame)类:

def __init__(self):

    wx.Frame.__init__(self, None, title="Nested Splitters",
                      size=(800,600))
    panel = MainPanel(self)
    self.Show()

如果名称 ==“ 主要”:

app = wx.App(False)

frame = MainFrame()

app.MainLoop()

1 个答案:

答案 0 :(得分:0)

问题在于,RegularPanel并未引用(即不知道)创建的self.text并存在于OtherPanel中。

有两种方法可以完成这项工作。在这种情况下,最简单的方法是在实例化实例时将OtherPanel实例传递给RegularPanel。然后,您可以引用另一个面板并访问其小部件。

这是经过修改的代码:

import wx

import wx.grid as gridlib

class RegularPanel(wx.Panel):

    def __init__(self, parent, other_panel):  # <-- New argument added

        wx.Panel.__init__(self, parent)
        frame = wx.GetTopLevelParent(self) 
        self.other_panel = other_panel # <-- Create a reference to other panel
        self.SetBackgroundColour("blue")
        button1 = wx.Button(self, label="change the text")
        self.Bind(wx.EVT_BUTTON, self.OnChange, id=button1.GetId())

    def OnChange(self, event):
        value = str(self.other_panel.text.GetLabel())  # <-- Use other panel to set text
        value = "this works"
        self.other_panel.text.SetLabel(str(value))


class OtherPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.SetBackgroundColour("white")
        self.text = wx.StaticText(self, -1, 'try to change this text', (40, 60))

class GridPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        self.grid = gridlib.Grid(self, style=wx.BORDER_SUNKEN)
        self.grid.CreateGrid(20,8)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(self.grid, 1, wx.EXPAND)
        self.SetSizer(sizer)


class MainPanel(wx.Panel):

    def __init__(self, parent):
        wx.Panel.__init__(self, parent)
        notebook = wx.Notebook(self)
        page = wx.SplitterWindow(notebook)
        notebook.AddPage(page, "Splitter")
        hSplitter = wx.SplitterWindow(page)
        panelOne = OtherPanel(hSplitter)
        panelTwo = GridPanel(hSplitter)
        hSplitter.SplitVertically(panelOne, panelTwo)
        hSplitter.SetSashGravity(0.5)
        panelThree = RegularPanel(page, panelOne)  # <-- Pass in other panel reference
        page.SplitHorizontally(hSplitter, panelThree)
        page.SetSashGravity(0.5)
        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(notebook, 1, wx.EXPAND)
        self.SetSizer(sizer)


class MainFrame(wx.Frame):

    def __init__(self):

        wx.Frame.__init__(self, None, title="Nested Splitters",
                          size=(800,600))
        self.panel = MainPanel(self)
        self.Show()

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

您也可以使用PubSub做这种事情。如果您对这种方法感兴趣,可以查看以下链接: