首页 > 解决方案 > 在SplitterWindow 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))

类其他面板(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()

标签: wxpython

解决方案


问题是它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用来做这种事情。如果您对这种方法感兴趣,可以查看以下链接:


推荐阅读