I have a problem using tkinter and object-oriented programming. I have 3 functions:
First I have the
v2()function which is a function that allows to open a second window through a button and show the data saved in the databaseMy second function is
agregar_postulantes(). This function saves the data in the database.My third function is the one that saves the data of
agregar_postulantes()and send it to thev2()function.
I want to emphasize that all the functions are in the same class and are declared outside the __init__().
A short version of the code:
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()
The error that appears is the following:
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'
the error is because I am calling the v2 function after saving the data using the other two functions
How can I call the v2 function afterwards and not show errors?