This spreadsheet is almost done, but there is some functionality that is driving me nuts.
For instance: a cell, for instance 'a0' is to have 'a0' as a string, but if something is entered like '4+5', that is also there. So, at any time, one could see the cell number a0 or they could click on it and have the equation show as well. Right now, I can make it show the equation, and evaluate it, or it can also show the cell number. But I can't make it so if the user goes back to that cell after showing the cell number that the equation will show. It will always show the cell number based on the event, but it retains the part that was in there because I don't want to 'delete' it.
How do I save the equation without showing it all the time? I'm thinking it should be saved to the dictionary I already have, but alas I'm stumped. Any ideas?
For instance: a cell, for instance 'a0' is to have 'a0' as a string, but if something is entered like '4+5', that is also there. So, at any time, one could see the cell number a0 or they could click on it and have the equation show as well. Right now, I can make it show the equation, and evaluate it, or it can also show the cell number. But I can't make it so if the user goes back to that cell after showing the cell number that the equation will show. It will always show the cell number based on the event, but it retains the part that was in there because I don't want to 'delete' it.
How do I save the equation without showing it all the time? I'm thinking it should be saved to the dictionary I already have, but alas I'm stumped. Any ideas?
Code:
from Tkinter import *
class spreadsheet(Frame):
"""
initialize columns and rows, default of 5
"""
def __init__(self, parent=None, numrow=5, numcol=5):
Frame.__init__(self, parent)
self.numrow = numrow
self.numcol = numcol
self.entriesDict = {} # an empty dictionary
self.makeWidgets(numrow, numcol)
def onEvent(self, event, cell):
""" define events, evaluate and show cell
"""
if event.num == 1:
data = self.entriesDict[cell]
data.insert(0, str(cell))
if event.num == 3:
obj = self.entriesDict[cell]
data = obj.get()
def returnKey(self, event, cell):
"""
define returnKey event
"""
obj = self.entriesDict[cell]
data = obj.get()
try:
result = eval(data)
obj.delete(0, 'end')
obj.insert(0, str(result))
except:
pass #nothing should crash the spreadsheet
def makeWidgets(self, numrow, numcol):
"""
define labels for rows and columns, use entry widget, assign events, create dictionary of
cell:widget pair
"""
dict = {}
w = 20
h = 1
rowLabel = ["", 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
for row in range(numrow):
for col in range(numcol):
if col == 0:
# label rows
labels = Label(root, width = 3, text = rowLabel[row])
labels.grid(row=row, column=col, padx = 2, pady = 2)
elif row == 0:
# label columns
labels = Label(root, width=3, text = str(col-1))
labels.grid(row=row, column=col, padx = 2, pady =2)
else:
# use entry widget
entrys = Entry(root, width=w)
entrys.grid(row=row, column=col)
cell = "%s%s" %(rowLabel[col], row)
self.entriesDict[cell] = entrys
# bind to left mouse click
entrys.bind('<Button-1>', lambda e, cell=cell: self.onEvent(e, cell))
# bind the object to a right mouse click
entrys.bind('<Button-3>', lambda e, cell=cell: self.onEvent(e, cell))
# bind the object to a return/enter press
entrys.bind('<Return>', lambda e, cell=cell: self.returnKey(e, cell))
# start curser in row a column 0
self.entriesDict['a1'].focus()
if __name__ == '__main__':
import sys
root = Tk()
root.title('S P R E A D S H E E T')
if len(sys.argv) != 3:
spreadsheet(root).grid()
else:
rows, cols = eval(sys.argv[1]), eval(sys.argv[2])
spreadsheet(root, rows, cols).grid()
root.mainloop()
Comment