I have a combobox in one "subscreen" and I am filtering the original dataframe with combo box value but I can't use resulting dataframe to show it in the treeview function or else where. I don't know how to call this function to use it else where.
This is my subscreen:
class MantPrev(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
super().__init__(parent)
self.tabla_data()
self.loc_combo()
def loc_combo(self):
conexion = CNX()
CombValue1 = ComboLoc()
calc_sinv = pd.DataFrame()
sql = " SELECT etc' "
try:
conexion.cursor.execute(sql)
calc_sinv = conexion.cursor.fetchall()
conexion.cerrar()
except:
col_name = ([desc[5] for desc in conexion.cursor.description])
self.algo_var = tk.StringVar()
self.algo_combo1 = ttk.Combobox(self, width=30)
self.algo_combo1['values'] = ComboLoc()
self.algo_combo1.set('')
self.algo_combo1.place(x=50, y=40) # grid(row=32, column=2, padx=0, pady=1)
self.algo_combo1.bind("<<ComboboxSelected>>", self.location)
self.algo_combo1.delete("0", tk.END)
def location(self, event):
df1 = CL001()
loc_selected = self.algo_combo1.get()
self.df2 = df1[(df1.LOCATION == loc_selected)]
return self.df2
# I managed to print self.df2 but I can't manage to use it in a different function
def tabla_data(self):
self.Sele = location()#####is not working# .to_numpy().tolist()
self.tablaO = ttk.Treeview(self,
columns=('a', 'b', 'c', 'd'), height='20',
selectmode=tk.NONE)
self.tablaO.grid(row=20, column=0, columnspan=3, pady=4)
self.tablaO.place(width=1000, height=300)
self.tablaO.place(relx=0.5, rely=0.5, anchor=CENTER)
self.tablaO.heading('#0', text='Localización')
self.tablaO.heading('#1', text='Descripción_MP')
self.tablaO.heading('#2', text='Fecha_Estatus_OT')
self.tablaO.heading('#3', text='Estatus_OT')
self.tablaO.heading('#4', text='Elemento')
self.tablaO.column('#0', width=140)
self.tablaO.column('#1', width=140)
self.tablaO.column('#2', width=140)
self.tablaO.column('#3', width=140)
self.tablaO.column('#4', width=140)
self.tablaO.delete(*self.tablaO.get_children()) # Delete all times inside the treeview
for p in self.Sele:
self.tablaO.insert('', 0, text=p[3],
values=(p[11], p[1], p[2], p[13]))
labl_1 = Label(self, text="Localización", width=24, font=("bold", 10))
labl_1.place(x=50, y=10)
labl_11 = Label(self, text="Elemento", width=24, font=("bold", 10))
labl_11.place(x=300, y=10)
labl_2 = Label(self, text="Fecha Inicio MP", width=20, font=("bold", 10))
labl_2.place(x=550, y=10)
labl_3 = Label(self, text="Fecha Fin MP", width=20, font=("bold", 10))
labl_3.place(x=750, y=10) # (x=ancho, y=altura )
if __name__ == "__main__":
To call a method from within the same class, you prefix it with self
e.g.:
self.Sele = self.location()
because location
is a method defined within your MantPrev
class; self
always refers to the class in which it is used. So when you call self.location()
you're effectively saying "call the method named location
that is defined in self
(this class)".
Also, you don't really need self
before df2
(honestly you don't need to declare df2
at all, as far as I can see - unless you're using it somewhere else in the class). It should be enough to simply return the data taken from df1
def location(self, _event=None):
df1 = CL001()
loc_selected = self.algo_combo1.get()
return df1[(df1.LOCATION == loc_selected)]
This answer does a good job explaining when to use self
vs. just using return