Python 面向对象设计问题&x27;Positantet';对象没有属性';表7;
我在使用tkinter和面向对象编程时遇到问题。 我有3个功能:Python 面向对象设计问题&x27;Positantet';对象没有属性';表7;,python,python-3.x,oop,tkinter,Python,Python 3.x,Oop,Tkinter,我在使用tkinter和面向对象编程时遇到问题。 我有3个功能: 首先,我有v2()函数,该函数允许通过按钮打开第二个窗口并显示保存在数据库中的数据 我的第二个函数是agregar\u postatantes()。此功能将数据保存在数据库中 我的第三个函数是保存agregar\u postatantes()的数据并将其发送到v2()函数的函数 我想强调的是,所有函数都在同一个类中,并且在\uuuu init\uuuu()之外声明 代码的简短版本: def v2(self): sel
v2()
函数,该函数允许通过按钮打开第二个窗口并显示保存在数据库中的数据
v2()
函数的函数
\uuuu init\uuuu()
之外声明
代码的简短版本:
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\u postatement
属性。由于您在v2
函数中的self
上创建了table\u positionante
属性,我猜您在调用fetch\u all()
之前没有定义self.table\u positionante
,或者没有调用v2()
这意味着你有几个选择:
fetch\u all()
中创建一些逻辑,检查是否存在self.tabla\u postatante
并仅在找到该属性时执行。这可以采用try
/的形式,但
语句除外
\uuuu init\uuuu()的调用中定义self.tabla\u postatante
属性
\uu init\uuu()
中创建self.tabla\u postatante
属性,因为如果您是,当函数查找它时,您不会得到ValueError
这给了你几个选择。一种方法是确保在实例化类时,\uu init\uu()
创建一个self.table\u postatante
属性,如下所示:
类名:
定义初始化(自):
#在此处创建属性
self.table_postatante=ttk.Treeview(tablaBD,columns=(“Name”、“last Name”、“id”)
...
如果这对您正在开发的应用程序没有意义,另一种选择是在fetch\u all()
中执行一些异常处理。例如,您可以执行以下操作:
如果len(行)!=0:
尝试:
self.tabla\u postatante.delete(*self.tabla\u postatante.get\u children())
对于行中的行:
self.tabla_postatante.insert(“”,END,values=row)
con.commit()
除:
#对此处不存在的此属性定义一些适当的响应
您的用例当然会确定哪种方法最有意义。希望这会有所帮助。您需要在其他方法使用它之前调用创建
tabla_postatement
的v2
。从这个示例中不清楚是否进行了调用。错误消息不清楚吗?例如,您了解