dmnfarrell / tkintertable

A pure Python library for adding tables to a Tkinter application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

cell binding

nanduOO5 opened this issue · comments

Hi I am new to python. Can you help me how to bind the cells so that when I edit them the values wont return to their previous values. I am using it to update excel files.

You'll have to give me an example of what you're trying to do.

Here I am trying to read a file from the user which can only be a spreadsheet. When I am trying to edit the cells they just revert back to their original state. I need to them to be in the changed state and can you also suggest me how to reflect the same changes in the file from which the values are being displayed.

from tkintertable import TableCanvas, TableModel
from tkintertable import *
from tkinter import *
from xl2dict import *

filepath = ''
def openFile(event=None):
global filepath
filepath = filedialog.askopenfilename(filetypes=[('Excel files','.xlsx'),('Excel 2009 files','.xlx'),('CSV files','*.csv')])
displayTables(filepath)

def displayTables(filepath):
master = Tk()
master.geometry("1024x720")
master.title('Spreadsheet UI')
obj = XlToDict()
a2d = obj.convert_sheet_to_dict(file_path=filepath)

data = {}
for i in range(0,len(a2d)):
        data.update({str(i):a2d[i]})
        
tframe = Frame(master)
tframe.pack(fill=BOTH,expand=1)
table = TableCanvas(tframe,data=data)
model = table.model
model.importDict(data)
#table.redraw()
table.show()
master.mainloop()

open_win = Tk()
open_win.geometry("200x100")
open_win.title("Spreadsheet UI")
btn = Button(open_win,text="Import file",command = openFile)
btn.pack()

You must not be updating the dict the way you need to. It doesn't look to me like you're doing anything to change it. setting data.update({str(i):a2d[i]}) just copies the other dict?

I have not done anything to update the dictionary. I can not understand how to retrieve the updated values from the cells. If I can understand that then I will something about updating the values. I need help with retrieving the values from the cells.

You should be able to retrieve the cell values this way:

data = table.model.data
cols = table.model.columnNames #get the current columns
value = data[row][col]

How would I know which row or col is updated by the user? That's the problem.

Maybe you could sub-class the table and override the drawCellEntry method and run the code you need inside that method.