我在使用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
函数并且不显示错误?
答案 0 :(得分:1)
正如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__()
属性,因为如果您使用的是该函数,则不会得到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
您的用例当然会确定哪种方法最有意义。希望这会有所帮助。