Akascape / CTkTable

Customtkinter Table widget (extension/add-on)
MIT License
281 stars 16 forks source link

How can we select rows? #29

Closed Nigacatch closed 1 year ago

Nigacatch commented 1 year ago

Hello, thank you for this project! I need to select some rows in the table to transfer from the table to the task history as completed tasks. How can I do this? thanks for your help

Akascape commented 1 year ago

@HassanAnari Try .get() method and it will return all the values. Then you can get the row values from that. Example:

all_values = table.get()
row_number = 2
row_values = all_values[row_number]
...
Nigacatch commented 1 year ago

@Akascape Thank you, this is the method for the coding part. I meant that the user can select rows in the graphical environment of the program. Is this possible?

Akascape commented 1 year ago

@HassanAnari Like this?

import customtkinter
from CTkTable import *

root = customtkinter.CTk()

row_nums = []
row_values = []

def show(cell):
    if cell["row"]==0:
        return # don't change header
    if cell["row"] not in row_nums:
        table.edit_row(cell["row"], fg_color=table.hover_color)
        row_nums.append(cell["row"])
        row_values.append(table.get()[cell["row"]])
    else:
        table.edit_row(cell["row"], fg_color=table.fg_color if cell["row"]%2==0 else table.fg_color2)
        row_nums.remove(cell["row"])
        row_values.remove(table.get()[cell["row"]])
    print(row_values)

value = [["A","B","C","D","E"],[1,2,3,4,5],[5,6,7,8,9],[0,0,0,0,0],[1,2,3,4,5]]

table = CTkTable(master=root, row=5, column=5, values=value, command=show, header_color="green")
table.pack(expand=True, fill="both", padx=20, pady=20)

root.mainloop()
Nigacatch commented 1 year ago

@Akascape Exactly! I added this code:

...
def delete():
    if len(row_nums) == 0:
        return
    else:
        for i in row_nums:
            # place of the function to transfer data to task history 
            row_nums.remove(i)
            table.delete_row(i)

btn = customtkinter.CTkButton(root, text='Delete', width=150, command=delete)
btn.pack(expand=True,pady=5, padx=5)
...

When one row is selected, it deletes correctly, but when multiple rows are selected, it doesn't delete correctly... Can you help me?

Akascape commented 1 year ago

@HassanAnari Ok, I have added some new functions which will make the work easier, please update the package once. I have implemented new select and delete_rows method.

import customtkinter
from CTkTable import *

root = customtkinter.CTk()

row_nums = []
deleted_values = []

def show(cell):
    if cell["row"] not in row_nums:
        table.select_row(cell["row"])
        row_nums.append(cell["row"])
        deleted_values.append(table.get_row(cell["row"]))
    else:
        table.deselect_row(cell["row"])
        row_nums.remove(cell["row"])
        deleted_values.remove(table.get_row(cell["row"]))

def delete():
    global row_nums
    if len(row_nums) == 0:
        return
    else:
        table.delete_rows(row_nums)
        row_nums = []
    print(deleted_values)

value = [["A","B","C","D","E"],[1,2,3,4,5],[0,0,0,0,0],[1,2,0,0,0],[1,2,3,4,5]]

table = CTkTable(master=root, row=5, column=5, values=value, command=show, header_color="green")
table.pack(expand=True, fill="both", padx=20, pady=20)

btn = customtkinter.CTkButton(root, text='Delete', width=150, command=delete)
btn.pack(expand=True,pady=5, padx=5)
root.mainloop()
Kelvin-Data commented 7 months ago

How about adding a row in Custom Tkinter Table?