在另一个类中使用类变量
大家好,
我目前正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的“选项卡”不是类变量,它们是函数
__init__
内的局部变量。另外,您不需要类变量,您需要实例变量。要读取和写入实例变量,您需要将它们作为self
的属性来访问,例如self1.pag1
,而不是通过写入它们的名称。您需要区分:
NovoRegisto.variable_name
)self
上的属性运算符进行访问(例如self.pag1
)。您可能应该阅读有关如何使用 Python 类 的更多信息。
作为补充说明,您最常希望使用
over类型(即间接或直接从内置
object
继承的所有类)新式类中可用的
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 ofself
, for exampleself1.pag1
, not by writing their name.You need to distinguish between:
NovoRegisto.variable_name
)self
(such asself.pag1
).You should probably read more about how Python classes should be used.
As an additional note, you'd most often want to use
over
which is available in new-style classes (i.e. all classes that indirectly or directly inherit from the builtin
object
)