python - OOP 设计问题 - 'postulantet' 对象没有属性 'tabla_postulante'
问题描述
我在使用 tkinter 和面向对象编程时遇到问题。我有 3 个功能:
首先,我有
v2()
一个功能,该功能允许通过按钮打开第二个窗口并显示保存在数据库中的数据我的第二个功能是
agregar_postulantes()
. 此函数将数据保存在数据库中。我的第三个函数是保存数据
agregar_postulantes()
并将其发送到函数的v2()
函数。
我想强调的是,所有函数都在同一个类中,并且在__init__()
.
代码的简短版本:
def v2(self):
self.tabla_postulante=ttk.Treeview(tablaBD,columns=("Name","last name","id")
self.tabla_postulante.heading("Name",text="Name")
self.tabla_postulante.heading("last name",text="last name")
self.tabla_postulante.heading("id",text="id")
self.tabla_postulante['show']='headings'
self.tabla_postulante.column("Name",width=100)
self.tabla_postulante.column("last name",width=100)
self.tabla_postulante.column("id",width=100)
self.fetch_all()
self.tabla_postulante.pack(fill=BOTH,expand=1)
def agregar_postulantes(self):
con = pymysql.connect(host="localhost", user="root",password="", database="postulantebd")
cur = con.cursor()
cur.execute("insert into postulantes values(%s, %s, %s)",(
self.name_var.get(),
self.lastname_var.get(),
self.id_var.get(),
))
con.commit()
self.fetch_all()
con.close()
def fetch_all(self):
con = pymysql.connect(host="localhost", user="root",password="", database="postulantebd")
cur = con.cursor()
cur.execute("select * from postulantes")
rows=cur.fetchall()
if len(rows)!=0:
self.tabla_postulante.delete(*self.tabla_postulante.get_children())
for row in rows:
self.tabla_postulante.insert('',END,values=row)
con.commit()
con.close()
出现的错误如下:
Traceback (most recent call last):
File "C:\Users\dimitri\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "C:\Users\dimitri\Documents\Programas de Yolo\practicadeinterfazypoo\tkinter\interfaz_tesis_poo.py", line 273, in agregar_postulantes
self.fetch_all()
File "C:\Users\dimitri\Documents\Programas de Yolo\practicadeinterfazypoo\tkinter\interfaz_tesis_poo.py", line 282, in fetch_all
self.tabla_postulante.delete(*self.tabla_postulante.get_children())
AttributeError: 'postulante' object has no attribute 'tabla_postulante'
错误是因为我在使用其他两个函数保存数据后调用 v2 函数之后如何调用该v2
函数而不显示错误?
解决方案
正如AttributeError
各州一样,self
似乎没有tabla_postulante
属性。由于您在函数中创建了一个table_postulante
属性,我猜您没有在中定义或者您没有在调用之前调用。self
v2
self.table_postulante
__init__()
v2()
fetch_all()
这应该意味着您有几个选择:
创建一些逻辑
fetch_all()
来检查是否存在self.tabla_postulante
并且仅在找到该属性时执行。这可以采用try
/except
语句的形式。或者,在实例化对象时
self.tabla_postulante
在调用中定义属性。__init__()
更新
关于实现我上面描述的内容的更多细节。我将假设您没有self.tabla_postulante
在 中的类上创建属性,因为如果您是,则在函数查找它时__init__()
您不会得到 a 。ValueError
所以这给了你几个选择。一种是简单地确保当您实例化您的类时__init__()
创建一个self.table_postulante
属性,如下所示:
class ClassName:
def __init__(self):
#create attribute here
self.table_postulante = ttk.Treeview(tablaBD,columns=("Name", "last name","id")
...
如果这对您正在开发的应用程序没有意义,另一种选择是在fetch_all()
. 例如,您可以执行以下操作:
if len(rows)!=0:
try:
self.tabla_postulante.delete(*self.tabla_postulante.get_children())
for row in rows:
self.tabla_postulante.insert('',END,values=row)
con.commit()
except:
#define some appropriate response to this attribute not existing here
您的用例当然会决定哪种方法最有意义。希望这可以帮助。
推荐阅读
- git - git lfs 不工作 - 仍然抛出错误 - 远程:错误:GH001:检测到大文件
- python - Python 新手:使用两个列表创建字典,其中一个列表多于一行
- uml - 如何在用例图中显示对象的条件
- swift - URLSession 返回脚本元素仍然是字符串编码和转义
- javascript - js只用于一个按钮
- java - Java不显示最后的输出
- python - 如何从 meson.build 中的 python 文件导入函数
- android - 主页和后退按钮在 android 模拟器像素 2 和像素 3 api 中不起作用
- conv-neural-network - 为 InceptionResnetV2 设置输入形状
- reactjs - OnClick 功能关闭一个模式并打开另一个