0

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__":

1 Answer 1

0

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

Sign up to request clarification or add additional context in comments.

10 Comments

Thank you. First bit works but still I get error when calling self.location()
def location(self,event): df1 = CL001() loc_selected=self.algo_combo1.get() return df1[(df1.LOCATION == loc_selected)] def tabla_data(self): self.Sele =self.location().to_numpy().tolist()
self.Sele =self.location().to_numpy().tolist() TypeError: location() missing 1 required positional argument: 'event'
Ah, I missed that - you can simply update the definition of location to set a default for the unused event parameter, or replace event with *args (shorthand for "this method accepts any number of positional arguments") - I'll update the answer. FYI: event is passed in by this binding: self.algo_combo1.bind("<<ComboboxSelected>>", self.location), but you aren't using it so it can be ignored/overridden safely.
Hi again JRiggles, if I get rid of event and self.algo_combo1.bind("<<ComboboxSelected>>", self.location) I get new error, saying the class hasn't got the attribute. I added the *args instead of event but then it throws error :
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.