【发布时间】:2021-02-14 16:20:26
【问题描述】:
我在使用 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 函数而不显示错误?
【问题讨论】:
-
您需要调用
v2,它会在其他使用它的方法之前创建tabla_postulante。从这个例子中不清楚是否进行了该调用。 -
错误信息是否不清楚?例如,您了解
attribute是什么吗? -
我认为tabla_postulante是v2中声明的属性。
标签: python python-3.x oop tkinter