0

I'm trying to create a py script that basically displays the data from an excel file in a tkinter window with grid layout. It create one textbox for each cell on the excel file. But when there are lots of cells, I cannot see them, because I have no scroolling implemented, and I have no idea how to implement them because the script generates 1 texbox for each cell...

here is the part of the code that generates the textboxes:

# Extracting columns from the data and creating text widget with some background color
column_names = df.columns
i=0
for j, col in enumerate(column_names):
    text = Text(window, width=20, height=1, font=('Arial', 10,'bold'), bg = "#9BC2E6")
    text.grid(row=i,column=j, sticky="ew")
    text.tag_configure("tag_name", justify='center')
    text.insert(INSERT, col)
    text.tag_add("tag_name", "1.0", "end")

# adding all the other rows into the grid
for i in range(n_rows):
    for j in range(n_cols):
        text = Text(window, width=len(str(df.loc[i][j])), height=1, font=('Times', 8))
        text.grid(row=i+1,column=j, sticky="ew")
        text.tag_configure("tag_name", justify='center')
        text.insert(INSERT, df.loc[i][j])
        text.tag_add("tag_name", "1.0", "end")
        cells[(i,j)] = text
for i in range(n_rows):
    if i >= len(buttons_list):
        button = Button(window, text="Copiar", bg = "#08ec9c", command=lambda row=i: copy_row(cells, n_cols, row))
        button.grid(row=i+1,column=n_cols)
        buttons_list.append(button)
    else:
        buttons_list[i].grid(row=i+1,column=n_cols)
    
    if i >= len(remove_buttons_list):
        remove_button = Button(window, text="Remover", bg = "#ac4c1e", command=lambda row=i: remove_row(i))
        remove_button.grid(row=i+1,column=n_cols+1)
        remove_buttons_list.append(remove_button)
    else:
        remove_buttons_list[i].grid(row=i+1,column=n_cols + 1)


        all_data = [df.iloc[i].values.tolist() for i in range(n_rows)]

how can I scroll trough the content the code generates? (the textboxes)

Rrios
  • 11
  • 2
  • what is `df`? Can your example included a tiny hard-coded definition of `df`? You also don't define several other variables, which makes it impossible to run this code. – Bryan Oakley Jan 27 '23 at 17:42
  • I think this is just a duplicate of [Adding a scrollbar to a group of widgets in Tkinter](https://stackoverflow.com/questions/3085696/adding-a-scrollbar-to-a-group-of-widgets-in-tkinter), isn't it? Basically you're just asking how to scroll a grid of widgets. – Bryan Oakley Jan 27 '23 at 17:57
  • df is dataframe from pandas – Rrios Jan 27 '23 at 19:02
  • I can post the full code if needed, its not so long only 200something lines – Rrios Jan 27 '23 at 19:03
  • No, we don't want "full code". We need a [mcve]. It's clear that you don't actually need a pandas dataframe to illustrate this problem. – Bryan Oakley Jan 27 '23 at 19:09
  • well, the widgets are created from the dataframe, and the code is basically load excel file into pandas as a dataframe, and create textboxes for each cell with this piece of code I posted, so... if you can reproduce it without a dataframe idk how – Rrios Jan 27 '23 at 19:29
  • Does the linked question solve the problem? If not, why? And yes, this is reproducible without a `df` because the core part of your question can be translate to: "How to give scrollbar to a group of widgets (in this case, `Text`)", the code for that does not require for us to have a `df` – Delrius Euphoria Jan 27 '23 at 20:03
  • I reproduced it simply by hard-coding the list of columns, and inserting strings into the widgets rather than data from a dataframe. – Bryan Oakley Jan 27 '23 at 21:07

0 Answers0