views:

173

answers:

1

I was trying to get my first wxWindow application to work and I ran into following difficulty:
I create wxPanel and add a wxNotebook object to it. Then I add a page to notebook created from another wxPanel object. How do I access a value of TextCtrl from first wxPanel in the second one?

import wx

class BasicApp(wx.App):
  def OnInit(self):
    frame = BasicFrame(None, -1, "Test App")
    panel = BasicPanel(frame, -1);
    frame.Show(True)
    self.SetTopWindow(frame)
    return True;

class BasicFrame(wx.Frame):
  def __init__(self, parent, ID, title):
    wx.Frame.__init__(self, parent, ID, title,
                     wx.DefaultPosition, wx.Size(400, 300))
    self.CreateStatusBar()

class BasicPanel(wx.Panel):
  def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id)

    self.lblText1 = wx.StaticText(self, -1, "Text1:");
    self.txtText1 = wx.TextCtrl(self, 1001, "Text1", size = wx.Size(140, -1));
    self.line1 = wx.BoxSizer(wx.HORIZONTAL);
    self.line1.Add(self.lblText1, 0, wx.EXPAND);
    self.line1.Add(self.txtText1, proportion=1, flag=wx.LEFT, border=5);

    self.nb = wx.Notebook(self, -1);
    tab1 = Tab1(self.nb, -1);
    self.nb.AddPage(tab1, "Tab1");

    self.sizer = wx.BoxSizer(wx.VERTICAL)
    self.sizer.Add(self.line1, 0, wx.EXPAND)
    self.sizer.Add(self.nb, 1, wx.EXPAND)

    self.SetSizer(self.sizer)
    self.SetAutoLayout(1)
    self.sizer.Fit(self)
    self.Show(1)

class Tab1(wx.Panel):
  def __init__(self, parent, id):
    wx.Panel.__init__(self, parent, id);

    self.lblText2 = wx.StaticText(self, -1, "Text2:");
    self.txtText2 = wx.TextCtrl(self, 1101, "Text2", size = wx.Size(140, -1));
    self.line1 = wx.BoxSizer(wx.HORIZONTAL);
    self.line1.Add(self.lblText2, 0, wx.EXPAND);
    self.line1.Add(self.txtText2, 0, wx.EXPAND);

    self.lblMessage = wx.StaticText(self, -1, "Message:");
    self.txtMessage = wx.TextCtrl(self, 1102, "", style = wx.TE_MULTILINE);

    self.cmdCreate = wx.Button(self, 1103, "Create");
    self.cmdCreate.Bind(wx.EVT_BUTTON, self.Create_OnClick)
    self.line3 = wx.BoxSizer(wx.HORIZONTAL);

    self.sizer = wx.BoxSizer(wx.VERTICAL)
    self.sizer.Add(self.line1, 0, wx.EXPAND)
    self.sizer.Add(self.lblMessage, 0, wx.EXPAND)
    self.sizer.Add(self.txtMessage, 1, wx.EXPAND)
    self.sizer.Add(self.cmdCreate, 0, wx.LEFT)

    self.SetSizer(self.sizer)
    self.SetAutoLayout(1)
    self.sizer.Fit(self)
    self.Show(1)
  def Create_OnClick(self, event):
    text1 = "";
    text2 = self.txtText2.GetValue();
    self.txtMessage.SetValue(text1 + " " + text2);

app = BasicApp(0)
app.MainLoop()

To be more specific I want to be able to access value of txtText1 in Create_OnClick() method. How could this be achieved?

+1  A: 

One solution is to pass the control to the constructor of the tab, then you can directly reference it. For example:

class Tab1(wx.Panel):
  def __init__(self, parent, id, textCtrl1):
    wx.Panel.__init__(self, parent, id);
    self.textCtrl1 = textCtrl1
  ...
  def Create_OnClick(self, event):
    text1 = self.textCtrl1

Another solution is to move the Create_OnClick handler to the base panel since it knows about all of the other panels (and thus, their children). Or, create a separate controller class that knows about the various widgets and can have handlers that act on behalf of them all.

Bryan Oakley
Thank you. The first way is acceptable for me.
artdanil