在另一个类中使用类变量 [英] Use class variable in another class

查看:277
本文介绍了在另一个类中使用类变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候大家,

我目前正在使用Python和wxPython开发应用程序. 在其中,我有一个对话框,其中填充了几个字段,以便在数据库中插入文档". 该对话框的布局基本上由wx.Notebook组成,带有几个选项卡",每个选项卡都包含某种字段.

I'm currently working on an app using Python and wxPython. In it I have a Dialog where several fields are filled in order to insert a "document" in a database. The layout of that Dialog consists basically of a wx.Notebook, with several "tabs", each containing some sort of fields.

# Dialog class
class NovoRegisto(wx.Dialog):
    def __init__(self,parent):
        wx.Dialog.__init__(self, parent, title='Registar Nova O.T.', size=(900,600))

        painel = wx.ScrolledWindow(self, -1, style=wx.VSCROLL|wx.HSCROLL)
        painel.SetScrollbars(0,30,0,500)
        notebook = wx.Notebook(painel)

        # create the page windows as children of the notebook
        pag1 = InfoOT(notebook)
        pag2 = Avaliacao(notebook)
        pag3 = Componentes(notebook)
        pag4 = Material(notebook)
        pag5 = OTsRelacionadas(notebook)

                          <...>
        # function to insert data in SQLite database
        def OnRegister(self,event):
                          <...>

# first tab class
class InfoOT(wx.Panel):
    def __init__(self, parent):
        wx.Panel.__init__(self, parent)

                          <...>

如您所见,我为Dialog本身提供了一个类(其定义由注册"按钮控制),然后为笔记本的每个选项卡"提供了不同的类.

As you can see, I have a class for the Dialog itself (with a definition controlled by a "Register" button) and then a different class for each of the "tabs" of the notebook.

现在,为了将数据提交到数据库,我必须有权访问"OnRegister"定义(属于Dialog的类)中的"tabs"变量.但是,我仍然没有找到一种方法.

Now, in order to submit the data to the database, I must have access to the "tabs" variables in the "OnRegister" definition (which belongs to the Dialog's class). However, I still haven't found a way to do that.

有人可以帮助我吗?我必须更改程序的结构吗?我这样做是因为这是我设法使笔记本工作的唯一方法...

Can anyone help me? Do I have to change my program's structure? I did it this way because it was the only way I managed to make the notebook work...

提前谢谢

推荐答案

您的标签"不是类变量,它们是函数__init__中的局部变量.另外,您不需要类变量,而需要实例变量.要读写实例变量,您需要将其作为self的属性(例如self1.pag1)进行访问,而无需输入其名称.

Your "tabs" aren't class variables, they are local variables inside the function __init__. Also you don't want class variables, you want instance variables. To read and write instance variables you need to access them as attributes of self, for example self1.pag1, not by writing their name.

您需要区分:

  • 函数局部变量-您在函数内分配的变量
  • 类变量-通过属性运算符(例如NovoRegisto.variable_name)访问的类属性
  • 实例变量-使用self(例如self.pag1)上的属性运算符访问的实例属性.
  • function local variables - variables that you assign within a function
  • class variables - class attributes that you access through the attribute operator (such as NovoRegisto.variable_name)
  • instance variables - instance attributes that you access by using the attribute operator on self (such as self.pag1).

您可能应该了解更多有关应如何使用 Python类的信息.

You should probably read more about how Python classes should be used.

作为附加说明,您最常想使用

As an additional note, you'd most often want to use

super(InfoOT, self).__init__(parent)

wx.Panel.__init__(self, parent)

在新型类中可用(即,所有从内置object间接或直接继承的类)

which is available in new-style classes (i.e. all classes that indirectly or directly inherit from the builtin object)

这篇关于在另一个类中使用类变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆