0

I'm trying to colorize a specific row in the output when I click on the "Sortiert" button". Is there any quick way to include this into my code? Another option would be that clicking the button deletes the entire row. I haven't found a solution for tkinter for now, maybe somebody has an idea or has had a similar problem.

import pandas as pd 
import tkinter as tk
from tkinter import *

eingabe = pd.read_excel("xxx.xlsx")
eingabe.drop(eingabe.columns[[0,1,2,3,4,5,9,10,11,12,17]], axis=1, inplace = True)
eingabe["Liefer-/Fälligkeitsdatum"] = eingabe["Liefer-/Fälligkeitsdatum"].dt.strftime("%d.%m.%Y")
writer = pd.ExcelWriter("yyy.xlsx")
eingabe.to_excel(writer,"Sheet1")
writer.save()

ausgabe = tk.Tk()
ausgabe.iconbitmap("ccc.ico")
ausgabe.title("Sortierliste")
ausgabe.geometry("1900x900")

df = pd.read_excel("aaa.xlsx")
df.drop(df.columns[[0]], axis=1, inplace = True) 
n_rows = df.shape[0]
n_cols = df.shape[1]

column_names = df.columns
i=0
for j, col in enumerate(column_names):
    text = Text(ausgabe, width=16, height=1.2, bg = "#9BC2E6")
    text.config(font=("Arial", 20))
    text.grid(row=i,column=j)
    text.insert(INSERT, col)
      
for i in range(n_rows):
    for j in range(n_cols):
        text = Text(ausgabe, width=16, height=1.2)
        text.config(font=("Arial", 20))
        text.grid(row=i+1,column=j)
        text.insert(INSERT, df.loc[i][j])
        button1=tk.Button(ausgabe, text="Sortiert", bg = "green")
        button1.grid(row=i+1,column=8)
  
ausgabe.mainloop()
FinneX
  • 5
  • 3
  • You probably want `button1 = Button(ausgabe, text="Sortiert", bg = "green", command = lambda: text.config(bg = "")`. – The Amateur Coder Jul 11 '22 at 11:36
  • 1
    Thank you! I think I've got a logical problem here. In my opinion I need defined buttons for every row depending of the index. With your method I can just colorize the last cell of my table. – FinneX Jul 11 '22 at 11:58
  • 1
    Note that you have created `n_cols` buttons in the same row and put them in the same cell. You should create the button outside the inner for loop. – acw1668 Jul 11 '22 at 12:14
  • You can add a tag to the indexes you want to change and then configure the tag. – Delrius Euphoria Jul 11 '22 at 12:17
  • @acw1668 Imo I need my inner loop for going through every row. I've now modified my inner loop to buttons with indexes like button[i]. Hopefully I just need to code an additional function to colorize the cells/row. – FinneX Jul 11 '22 at 12:36
  • I did not tell you to remove the inner for loop, I just said *"create the button outside the inner for loop"*. – acw1668 Jul 11 '22 at 12:43
  • @FinneX, I guess this should solve that problem: `button1 = Button(ausgabe, text="Sortiert", bg = "green", command = lambda text=text: text.config(bg = "")`. This will create a new value for each `text` at runtime, so all the cells would be colorized. See https://stackoverflow.com/questions/69334341/tkinter-create-clickable-labels-in-for-loop. – The Amateur Coder Jul 11 '22 at 15:10

1 Answers1

0

You can simply use a list to store those Text boxes in a row and pass this list to a function associated to the button of the corresponding row. Then you can go through the list inside the function to change the background color of those Text boxes:

def change_color(widgets, color):
    for widget in widgets:
        widget.config(bg=color)

for i in range(n_rows):
    widgets = []  # list to store the text boxes in this row
    for j in range(n_cols):
        text = Text(ausgabe, width=16, height=1.2)
        text.config(font=("Arial", 20))
        text.grid(row=i+1, column=j)
        text.insert(INSERT, df.loc[i][j])
        widgets.append(text) # add the text box to the list
    button1 = tk.Button(ausgabe, text="Sortiert", bg="green", command=lambda w=widgets: change_color(w, "green"))
    button1.grid(row=i+1, column=n_cols)

Note that I have moved the creation of the Sortiert button outside the inner for loop.

acw1668
  • 40,144
  • 5
  • 22
  • 34
  • Awesome, thank you for your help! Now I am on a good way of finishing my first coding project :) – FinneX Jul 12 '22 at 05:15