简体   繁体   English

在python中的对象之间传递数据

[英]Pass data between objects in python

I'm new to python and I'm not sure how to pass data between objects. 我是python的新手,我不确定如何在对象之间传递数据。 Below is a tabbed program using python and wxwidgets. 下面是一个使用python和wxwidgets的选项卡式程序。 How would I be able to access the maintxt instance from the GetText method since their in different classes? 我怎样才能从GetText方法访问maintxt实例,因为它们在不同的类中?

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()

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 有关此设计的更多信息: http//en.wikipedia.org/wiki/Model%E2%80%93View%E2%80%93Controller

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM