我有一个有两页的平面笔记本。我希望在按下按钮的第2页上的文本ctrl中显示第1页上的文本Ctrl中输入的文本。当我添加'self.secondTab = SecondTab(self)'时,我得到另一个文本框叠加在第1页上,文本在按下按钮时转移到该框。有一些小我不知道,请帮忙。
import wx
import wx.lib.agw.flatnotebook as fnb
class FirstTab(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
self.secondTab = SecondTab(self)
sizer = wx.BoxSizer(wx.VERTICAL)
self.txtOne = wx.TextCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
self.btnOne = wx.Button(self, -1, "Transfer text to Tab 2")
self.Bind(wx.EVT_BUTTON, self.sendText, self.btnOne)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.txtOne, 0, wx.ALL, 5)
sizer.Add(self.btnOne, 0, wx.ALL, 5)
self.SetSizer(sizer)
def sendText(self, event):
text = self.txtOne.GetValue()
self.secondTab.txtTwo.WriteText(text)
class SecondTab(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
self.parent = parent
sizer = wx.BoxSizer(wx.VERTICAL)
self.txtTwo = wx.TextCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,"Notebook", size=(300,300))
panel = wx.Panel(self)
notebook = wx.Notebook(panel)
page1 = FirstTab(notebook)
page2 = SecondTab(notebook)
notebook.AddPage(page1, "Tab 1")
notebook.AddPage(page2, "Tab 2")
sizer = wx.BoxSizer()
sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
self.Layout()
if __name__ == "__main__":
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()
答案 0 :(得分:0)
致电时
self.secondTab = SecondTab(self)
您正在构建SecondTab类的另一个实例。你不想这样做,你想引用你在Frame中构建的实例。
global frame #reference frame constructed in main
frame.page1.txtTwo.WriteText(text)
当然,您需要将帧设置为全局,因此您可以对其进行修改。
答案 1 :(得分:0)
对于任何有兴趣的人,我通过使用pubsub克服了这个障碍。我跟随迈克·德里斯科尔的例子来自他的blog
import wx
from wx.lib.pubsub import pub
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, -1,"Notebook", size=(300,300))
panel = wx.Panel(self)
notebook = wx.Notebook(panel)
page1 = FirstTab(notebook)
page2 = SecondTab(notebook)
notebook.AddPage(page1, "Tab 1")
notebook.AddPage(page2, "Tab 2")
sizer = wx.BoxSizer()
sizer.Add(notebook, 1, wx.ALL|wx.EXPAND, 5)
panel.SetSizer(sizer)
self.Layout()
class FirstTab(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
sizer = wx.BoxSizer(wx.VERTICAL)
self.txtOne = wx.TextCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
self.btnOne = wx.Button(self, -1, "Transfer text to Tab 2")
self.Bind(wx.EVT_BUTTON, self.sendText, self.btnOne)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.txtOne, 0, wx.ALL, 5)
sizer.Add(self.btnOne, 0, wx.ALL, 5)
self.SetSizer(sizer)
def sendText(self, event):
text = self.txtOne.GetValue()
pub.sendMessage("panelListener", message = text)
class SecondTab(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, -1)
pub.subscribe(self.myListener, "panelListener")
sizer = wx.BoxSizer(wx.VERTICAL)
self.txtTwo = wx.TextCtrl(self, -1, style=wx.TE_PROCESS_ENTER)
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(self.txtTwo, 0, wx.ALL, 5)
self.SetSizer(sizer)
def myListener(self, message):
self.txtTwo.WriteText(message)
if __name__ == "__main__":
app = wx.App()
frame = Frame()
frame.Show()
app.MainLoop()