English 中文(简体)
Getting value of TextCtrl from a different wxPanel
原标题:

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?

最佳回答

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.

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签