| | |
Tkinter Spreadsheet
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
Spreadsheets are really not that difficult to make with Python. Here is the beginning of a simple Tkinter based spreadsheet program:
[php]# the beginning of a simple Tkinter spreadsheet
import Tkinter as tk
root = tk.Tk()
root.title('Tkinter spreadsheet phase 1')
def click(event, cell):
# can do different things with right (3) and left (1) mouse button clicks
root.title("you clicked mouse button %d in cell %s" % (event.num, cell))
# test right mouse button for equation solving
# eg. data = '=9-3' would give 6
if event.num == 3:
# entry object in use
obj = dict[cell]
# get data in obj
data = obj.get()
# if it starts with '=' it's an equation
if data.startswith('='):
eq = data.lstrip('=')
print data, eq
try:
# solve the equation
result = eval(eq)
#print result, type(result) # test
# remove equation data
obj.delete(0, 'end')
# update cell with equation result
obj.insert(0, str(result))
except:
pass
def key_r(event, cell):
# return/enter has been pressed
data = dict[cell].get() # get text/data in given cell
#print cell, dict[cell], data # test
root.title("cell %s contains %s" % (cell, data))
# create a dictionary of key:value pairs
dict = {}
w = 20
h = 1
alpha = ["", 'A', 'B', 'C', 'D', 'E', 'F']
for row in range(5):
for col in range(5):
if col == 0:
# create row labels
label1 = tk.Label(root, width=3, text=str(row))
label1.grid(row=row, column=col, padx = 2, pady=2)
elif row == 0:
# create column labels
label1 = tk.Label(root, width=w, text=alpha[col])
label1.grid(row=row, column=col, padx = 2, pady=2)
else:
# create entry object
entry1 = tk.Entry(root, width=w)
# place the object
entry1.grid(row=row, column=col) #, padx = 2, pady=2)
# create a dictionary of cell:object pair
cell = "%s%s" % (alpha[col], row)
dict[cell] = entry1
# bind the object to a left mouse click
entry1.bind('<Button-1>', lambda e, cell=cell: click(e, cell))
# bind the object to a right mouse click
entry1.bind('<Button-3>', lambda e, cell=cell: click(e, cell))
# bind the object to a return/enter press
entry1.bind('<Return>', lambda e, cell=cell: key_r(e, cell))
#print dict # test
# set the focus on cell A1
dict['A1'].focus()
root.mainloop()
[/php]Right now I am using the right mouse button click to evaluate equations starting with '=', for instance a cell data of '=355/113.0' would give the approximation of pi. Still to do --> summing rows or columns of cells using something like '=sum(A1..A4)' needs to be parsed and evaluated. You also have to add a data save and load, plus a few more items. Give me an idea of what you need. Hopefully someone else will chime in too!
[php]# the beginning of a simple Tkinter spreadsheet
import Tkinter as tk
root = tk.Tk()
root.title('Tkinter spreadsheet phase 1')
def click(event, cell):
# can do different things with right (3) and left (1) mouse button clicks
root.title("you clicked mouse button %d in cell %s" % (event.num, cell))
# test right mouse button for equation solving
# eg. data = '=9-3' would give 6
if event.num == 3:
# entry object in use
obj = dict[cell]
# get data in obj
data = obj.get()
# if it starts with '=' it's an equation
if data.startswith('='):
eq = data.lstrip('=')
print data, eq
try:
# solve the equation
result = eval(eq)
#print result, type(result) # test
# remove equation data
obj.delete(0, 'end')
# update cell with equation result
obj.insert(0, str(result))
except:
pass
def key_r(event, cell):
# return/enter has been pressed
data = dict[cell].get() # get text/data in given cell
#print cell, dict[cell], data # test
root.title("cell %s contains %s" % (cell, data))
# create a dictionary of key:value pairs
dict = {}
w = 20
h = 1
alpha = ["", 'A', 'B', 'C', 'D', 'E', 'F']
for row in range(5):
for col in range(5):
if col == 0:
# create row labels
label1 = tk.Label(root, width=3, text=str(row))
label1.grid(row=row, column=col, padx = 2, pady=2)
elif row == 0:
# create column labels
label1 = tk.Label(root, width=w, text=alpha[col])
label1.grid(row=row, column=col, padx = 2, pady=2)
else:
# create entry object
entry1 = tk.Entry(root, width=w)
# place the object
entry1.grid(row=row, column=col) #, padx = 2, pady=2)
# create a dictionary of cell:object pair
cell = "%s%s" % (alpha[col], row)
dict[cell] = entry1
# bind the object to a left mouse click
entry1.bind('<Button-1>', lambda e, cell=cell: click(e, cell))
# bind the object to a right mouse click
entry1.bind('<Button-3>', lambda e, cell=cell: click(e, cell))
# bind the object to a return/enter press
entry1.bind('<Return>', lambda e, cell=cell: key_r(e, cell))
#print dict # test
# set the focus on cell A1
dict['A1'].focus()
root.mainloop()
[/php]Right now I am using the right mouse button click to evaluate equations starting with '=', for instance a cell data of '=355/113.0' would give the approximation of pi. Still to do --> summing rows or columns of cells using something like '=sum(A1..A4)' needs to be parsed and evaluated. You also have to add a data save and load, plus a few more items. Give me an idea of what you need. Hopefully someone else will chime in too!
Last edited by Ene Uran; Feb 13th, 2007 at 2:29 pm. Reason: spell
drink her pretty
![]() |
Similar Threads
- Projects for the Beginner (Python)
- Need Help with Tkinter (Python)
- Spreadsheet App for iWork (Mac Rumors and Reports)
- Colors and a spreadsheet (Visual Basic 4 / 5 / 6)
Other Threads in the Python Forum
- Previous Thread: Extract Recipe Data
- Next Thread: just for fun
Views: 3675 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for Python
anti approximation array avogadro beginner builtin cipher clear client code color converter countpasswordentry cturtle curved def dictionary drive dynamic examples excel file float format frange ftp function gui homework import input java lapse library line lines linux list lists loop microcontroller mouse multiple mysqldb mysqlquery newb number numbers output parsing path port prime program programming projects py2exe pygame pymailer pyqt python random recursion recursive redirect script scrolledtext singleton socket sqlite ssh stderr string strings subprocess sum syntax table terminal text textarea thread threading time tkinter tlapse tuple tutorial twoup ubuntu unicode unix urllib urllib2 variable web-scrape wikipedia windows word wxpython






