在Python中的对象之间传递数据
我是 python 新手,不知道如何在对象之间传递数据。下面是一个使用 python 和 wxwidgets 的选项卡式程序。由于它们位于不同的类中,我如何能够从 GetText 方法访问 maintxt 实例?
谢谢。
…………
#!/usr/bin/env python
import wx
class PageText(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()
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 PageText(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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来您可能将逻辑与演示混合在一起。您也许应该有一个模型类网络来描述您的域(页面?)的行为,然后将这些类的实例传递给表示类的初始值设定项,以便它们知道它们所代表的模型。
有关此设计的更多信息:http://en.wikipedia。 org/wiki/Model%E2%80%93View%E2%80%93Controller
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