在另一个类中使用类变量

发布于 2024-10-15 09:43:12 字数 1270 浏览 1 评论 0原文

大家好,

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

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

                          <...>

正如您所看到的,我有一个对话框本身的类(其定义由“注册”按钮控制),然后为笔记本的每个“选项卡”有一个不同的类。

现在,为了将数据提交到数据库,我必须能够访问“OnRegister”定义中的“tabs”变量(属于对话框的类)。但是,我仍然没有找到一种方法来做到这一点。

谁能帮助我吗?我必须改变程序的结构吗?我这样做是因为这是我设法使笔记本工作的唯一方法......

提前谢谢你

Greetings everyone,

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)

                          <...>

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.

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...

Thank you in advance

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

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

发布评论

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

评论(1

我最亲爱的 2024-10-22 09:43:12

您的“选项卡”不是类变量,它们是函数 __init__ 内的局部变量。另外,您不需要类变量,您需要实例变量。要读取和写入实例变量,您需要将它们作为 self 的属性来访问,例如 self1.pag1,而不是通过写入它们的名称。

您需要区分:

  • 函数局部变量 - 您在函数中分配的
  • 变量 类变量 - 您通过属性运算符访问的类属性(例如 NovoRegisto.variable_name
  • 实例变量 - 您在函数中分配的实例属性通过使用 self 上的属性运算符进行访问(例如 self.pag1)。

您可能应该阅读有关如何使用 Python 类 的更多信息。

作为补充说明,您最常希望使用

super(InfoOT, self).__init__(parent)

over类型(即间接或直接从内置 object 继承的所有类)

wx.Panel.__init__(self, parent)

新式类中可用的

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.

You need to distinguish between:

  • 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).

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)

over

wx.Panel.__init__(self, parent)

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

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