wxpython - 在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()
解决方案
问题是它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
用来做这种事情。如果您对这种方法感兴趣,可以查看以下链接:
推荐阅读
- bitnami - Bitnami 中的 server.js 在哪里?
- apache - Apache 无法使用 ProxyPass 托管 ASP.NET Core 应用程序
- mysql - 在where子句MySQL中转换日期
- linux - 如何在一台服务器中的 Apache 和另一台服务器中的 Tomcat 之间配置 AJP
- ios - 布局帮助适合任何屏幕
- css - 有没有办法通过主题的附加 CSS 界面添加新的 div 类?
- javascript - 如果数组中的连续数字相隔相同的大小,请输入
- python - 为数学数据插补python创建一个函数
- phpunit - 如何在最新版本的 PHPUnit 中扩展 \PHPUnit\TextUI\Command::createRunner?
- php - 我使用 php ajax 从服务器获得的链接在我的网页上不起作用