Home > Enterprise >  Stop Comboboxes from stacking up on top of the other
Stop Comboboxes from stacking up on top of the other

Time:07-27

I applied this solution in my code and noticed that when you open another file in the treeview, the Combobox stack up on top of each other.

My Treeview is different because it opens excel files on it. I simply copy and pasted the solution after Ttree.insert("", "end", values=row)

Code:

def clear_tree():
    Ttree.delete(*Ttree.get_children())

def open_file():
    namefile= filedialog.askopenfilename(
        initialdir="C:",
        title="Open a File",
        filetype=(("xlsx files", "*.xlsx"), ("All files", "*.*"))
    )
    namefile= r"{}".format(namefile)
    df = pd.read_excel(namefile)

    # clears the treeview
    clear_tree()
    # Set up new tree
    Ttree['column'] = list(df.columns)
    Ttree['show'] = "headings"
    # loop in the column list
    for column in Ttree['column']:
        Ttree.heading(column, text=column)
    # putting data in the treeview
    df_columns = df.to_numpy().tolist()
    # loop the data and putting them on the screen
    for row in df_columns:
        Ttree.insert("", "end", values=row)
    def create_column_combox():  # Creating the select field and value Combobox
        def populate_values_combo(e=None):  # populate values combobox with unique column values
            state = 'normal'
            combo_values = []
            if col_combo.get() == 'All':  # if option All selected
                state = 'disabled'  # disabling the option to select values
                select_value()
            else:
                combo_values = ['All']   df[
                    col_combo.get()].unique().tolist()  # adding option All to values

            col_values_combo.config(values=combo_values, state=state)

        def select_value(e=None):  # cleaning tree and populating with df values
            Ttree.delete(*Ttree.get_children())
            if col_combo.get() == 'All' or col_values_combo.get() == 'All':  # if All selected as column
                # or value
                col_values_combo.current(0)  # setting values to All
                [Ttree.insert("", "end", text=str(index), values=list(row)) for index, row in df.iterrows()]
            else:
                for index, row in df.loc[df[col_combo.get()].eq(col_values_combo.get())].iterrows():
                    Ttree.insert("", "end", text=index, values=list(row))

        columns = ['All']   list(df.columns)  # Adding option All to list of columns
        column_combo_label = Label(Fframe, text='Selecionar Coluna')
        column_combo_label.pack()
        col_combo = ttk.Combobox(Fframe, values=columns, state='readonly')
        col_combo.pack()
        col_combo.bind("<<ComboboxSelected>>", populate_values_combo)
        volume_combo_label = Label(Fframe, text='Selecionar Valor')
        volume_combo_label.pack()
        col_values_combo = ttk.Combobox(Fframe, state='disabled')
        col_values_combo.pack()
        col_values_combo.bind("<<ComboboxSelected>>", select_value)

    create_column_combox()

Deleting the Combobox and putting it back again everytime I open a new file will make it stop ? How to stop them from staking up ?

CodePudding user response:

You need to create the two comboboxes outside open_file() function and update them inside the function:

def open_file():
    namefile= filedialog.askopenfilename(
        initialdir="C:",
        title="Open a File",
        filetype=(("xlsx files", "*.xlsx"), ("All files", "*.*"))
    )
    df = pd.read_excel(namefile)

    # clears the treeview
    clear_tree()
    # Set up new tree
    Ttree['column'] = list(df.columns)
    Ttree['show'] = "headings"
    # loop in the column list
    for column in Ttree['column']:
        Ttree.heading(column, text=column)
    # putting data in the treeview
    df_columns = df.to_numpy().tolist()
    # loop the data and putting them on the screen
    for row in df_columns:
        Ttree.insert("", "end", values=row)

    # update comboboxes
    columns = ['All']   list(df.columns)  # Adding option All to list of columns
    col_combo['values'] = columns
    col_values_combo.config(values=('All'), state='disabled')

    col_combo.set('')
    col_values_combo.set('')

    def populate_values_combo(e):  # populate values combobox with unique column values
        state = 'readonly'
        combo_values = []
        if col_combo.get() == 'All':  # if option All selected
            state = 'disabled'  # disabling the option to select values
            select_value()
        else:
            combo_values = ['All']   df[col_combo.get()].unique().tolist()  # adding option All to values

        col_values_combo.config(values=combo_values, state=state)

    def select_value(e):  # cleaning tree and populating with df values
        Ttree.delete(*Ttree.get_children())
        if col_combo.get() == 'All' or col_values_combo.get() == 'All':  # if All selected as column
            # or value
            col_values_combo.current(0)  # setting values to All
            for index, row in df.iterrows():
                Ttree.insert("", "end", text=index, values=list(row))
        else:
            for index, row in df.loc[df[col_combo.get()].eq(col_values_combo.get())].iterrows():
                Ttree.insert("", "end", text=index, values=list(row))

    col_combo.bind("<<ComboboxSelected>>", populate_values_combo)
    col_values_combo.bind("<<ComboboxSelected>>", select_value)

...

column_combo_label = Label(Fframe, text='Selecionar Coluna')
column_combo_label.pack()

col_combo = ttk.Combobox(Fframe, state='readonly')
col_combo.pack()

volume_combo_label = Label(Fframe, text='Selecionar Valor')
volume_combo_label.pack()

col_values_combo = ttk.Combobox(Fframe, state='disabled')
col_values_combo.pack()

...
  • Related