在两个 wxpython 选项卡之间共享变量

发布于 2024-11-16 09:33:00 字数 1430 浏览 1 评论 0原文

我已经问过这个问题,但它的表述不是很好。

作为python

和 GUI 编程的新手,

我有一个包含许多 python 文件的大型项目。文件 a.py 定义了一个名为 fobject 的类,我正在使用 python 2.5

文件 b.py 和 c.py 有名为 BProject 和 CProject 的类,它们有一个 fobject 对象作为参数。这些是基于 wx python 的程序中的页面。

我已在 b.py 中使用 import CProject (在 c.py 中定义)。我在 CProject 中有一个列表,我使用 wx python GUI 填充它。接下来,我调用 BProject 中定义的函数 BRun,该函数在内部调用 CProject 中的 CRun 函数,即。在 c.py 中

在此 CRun 中,我想操作列表,但此时列表始终为空。为什么会这样呢?

考虑到约束我不能更改定义 fobject 的 a.py 任何内容,我应该做什么?

file : c.py

def Instance(fObject):
    return test_page_CProject(fObject)


class CProject(object):

    def __init__(self, fObject):
        self.fObj = fObject
        self.IntList  =  []
        ##snip

    def  OnIntSelectBtnPress(self,parent):
        print ":self.IntList"
        print self.IntList
        self.listBoxIntSelect.InsertItems(self.IntList,0)
        print self.IntList

    def OnIntRun(self):
        IntLModeList = self.IntListIntMode
        #snip

file b.py

def Instance(fObject):
    return BProject(fObject)

class BProject(object):

    def __init__(self, fObject):
        self.FObj = fObject
        #snip
        Object = __import__('CProject')
        #snip

        self.intObject = Object.Instance(self.FObj)
        self.intObject.OnIntRun()

当 CPython.OnIntRun 被调用时, self.IntList 是空的,而它不应该是空的

I have asked this question but it was not very well formulated.

persistence of objects in python

As a newbie to both python, and GUI programming

I have a large project which contains a number of python files. File a.py defines a class called fobject I am using python 2.5

File b.py and c.py have classes called BProject and CProject which have an object of fobject as parameter. These are pages in wx python based program.

I have included using import CProject (defined in c.py) in b.py. I have a list in CProject which I fill using wx python GUI. Next I call a function BRun defined in BProject which internally calls a CRun Function in CProject ie. in c.py.

In this CRun I want to manipulate the list but list is always empty at this time. Why is this so?

What should I do given the constraint is I can't change anything a.py in which fobject is defined ?

file : c.py

def Instance(fObject):
    return test_page_CProject(fObject)


class CProject(object):

    def __init__(self, fObject):
        self.fObj = fObject
        self.IntList  =  []
        ##snip

    def  OnIntSelectBtnPress(self,parent):
        print ":self.IntList"
        print self.IntList
        self.listBoxIntSelect.InsertItems(self.IntList,0)
        print self.IntList

    def OnIntRun(self):
        IntLModeList = self.IntListIntMode
        #snip

file b.py

def Instance(fObject):
    return BProject(fObject)

class BProject(object):

    def __init__(self, fObject):
        self.FObj = fObject
        #snip
        Object = __import__('CProject')
        #snip

        self.intObject = Object.Instance(self.FObj)
        self.intObject.OnIntRun()

When CPython.OnIntRun is called the self.IntList is empty when it should'nt be

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水波映月 2024-11-23 09:33:00

我不太明白这一点,但通常您需要在实例化类时传递数据。否则,您可以使用 PubSub 在类之间传递信息,或者您可以创建一个简单的本地 Web 服务器,每个选项卡定期检查新数据并自行更新。

I'm not following this very well, but normally you need to pass the data in when you instantiate the class. Otherwise, you can use PubSub to pass the information between classes or you could create a simple local web server that each tab checks periodically for new data and updates itself.

不奢求什么 2024-11-23 09:33:00

如果您的 fObject 维护对 bProject 和 cProject 的引用,您是否不能在 fObject 中编写一个可以从 bProject 调用的 send_to_c(msg) 函数(反之亦然)?

If your fObject maintains the references to bProject and cProject, couldn't you write a send_to_c(msg) function in your fObject, that you could call from your bProject (and vice versa)?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文