Pass data between objects in python
I'm new to python and I'm not sure how to pass data between objects. Below is a tabbed program using python and wxwidgets. How would I be able to access the maintxt instance from the GetText method since their in different classes?
Thanks.
........
#!/usr/bin/env python
import wx
class PageT开发者_Python百科ext(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.maintxt = wx.TextCtrl(self, style=wx.TE_MULTILINE, pos=(0, 40), size=(850,320))
self.Show(True)
class PageList(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent)
self.bPutText = wx.Button(self, id=-1, label='Put Text', pos=(855, 40), size=(75, 30))
self.bPutText.Bind(wx.EVT_LEFT_DOWN, self.GetText)
def GetText(self, event):
# Write text into maintxt
class MainFrame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None, title="ADMIN")
p = wx.Panel(self)
nb = wx.Notebook(p)
vPageText = PageText(nb)
vPageList = PageList(nb)
nb.AddPage(vPageText, "Edit Text")
nb.AddPage(vPageList, "Book List")
sizer = wx.BoxSizer()
sizer.Add(nb, 1, wx.EXPAND)
p.SetSizer(sizer)
if __name__ == "__main__":
app = wx.App()
MainFrame().Show()
app.MainLoop()
It sounds like you might be mixing logic with presentation. You should perhaps have a network of model classes that describe the behaviors of your domain (pages?) and then pass instances of those classes to the initializers of your presentation classes, so they know which models they are representing.
More about this design: http://en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller
精彩评论