The spreadsheet is due today and i needed help with the last two parts of the spreadsheet, so I went to my teacher. My spreedsheet was working BETTER before he made me change it. Now it prints a widget address in one of the cells for no apparant reason (I can't locate why the event is doing that), and it doesn't evaluate.
He wants me to use 2 dictionaries:
One that holds the cell number and expression, and
Two that holds the result of evaluating the expression.
He wrote out pseudocode for the evaluating process, and it isn't working at all.
When mouse clicking in the cell of the spreadsheet, the equation should show, if the equation was initially evaluated.
These are the two things I would like help with, because I will never get the third part (this thing is due tonight), where one references a cell with an equation or two cells and they are evaluated.
I'm mostly upset because the way I had it previously works better than what it is doing now. Here is the code, HELP!!!!! -
from Tkinter import *
-
class spreadsheet(Frame):
-
"""
-
initialize columns and rows, default of 5
-
"""
-
def __init__(self, parent=None, nr=4, nc=4):
-
Frame.__init__(self, parent)
-
self.numrow = nr
-
self.numcol = nc
-
self.entriesDict = {} # an empty dictionary
-
self.makeWidgets(nr, nc)
-
def onEvent(self, event, cellName, cellEntry):
-
""" define events, evaluate and show cell
-
"""
-
if event.num == 1:
-
#should retreive equation from dictionary then print it
-
equation = self.entriesDict[cellName]
-
cellEntry.insert(0, equation)
-
-
def returnKey(self, event, cellName, cellEntry):
-
"""
-
define returnKey event
-
"""
-
"""
-
self.entriesDict[cellName] = cellEntry.get()
-
try:
-
result = eval(data, self.entriesDict)
-
cellData.delete(0, 'end') #delete all text in the widget
-
cellData.insert(0, str(result)) #replace with result
-
except:
-
pass #nothing should crash the spreadsheet
-
"""
-
self.entriesDict[cellName] = cellEntry.get()
-
dictNew={}
-
for keys in self.entriesDict:
-
expr = self.entriesDict[keys]
-
#evaluate expr using dictNew
-
result=eval(expr, self.entriesDict)
-
#put result in dictNew under key
-
cellData.insert(0, str(result))
-
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
-
cellEntry = Entry(root, width=w)
-
cellEntry.grid(row=row, column=col)
-
cellName = "%s%s" %(rowLabel[row], col-1)
-
self.entriesDict[cellName] = cellEntry
-
# bind to left mouse click
-
cellEntry.bind('<Button-1>', lambda e, cellName=cellName: self.onEvent(e, cellName, cellEntry))
-
# bind the object to a right mouse click
-
cellEntry.bind('<Tab>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
-
# bind the object to a return/enter press
-
cellEntry.bind('<Return>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
-
-
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()
-
-
2 1766
I hope this gets you going in the right direction: - from Tkinter import *
-
class spreadsheet(Frame):
-
"""
-
initialize columns and rows, default of 5
-
"""
-
def __init__(self, parent=None, nr=4, nc=4):
-
Frame.__init__(self, parent)
-
self.numrow = nr
-
self.numcol = nc
-
self.entryValueDict = {} # an empty dictionary
-
self.entryFormulaDict = {} # an empty dictionary
-
self.makeWidgets(nr, nc)
-
-
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
-
cellEntry = Entry(root, width=w)
-
cellEntry.grid(row=row, column=col)
-
cellName = "%s%s" %(rowLabel[row], col-1)
-
self.entryValueDict[cellName] = cellName # just testing; should be ""
-
self.entryFormulaDict[cellName] = ""
-
# bind to left mouse click
-
cellEntry.bind('<Button-1>', lambda e, cellName=cellName: self.onEvent(e, cellName, cellEntry))
-
# bind the object to a right mouse click
-
cellEntry.bind('<Tab>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
-
# bind the object to a return/enter press
-
cellEntry.bind('<Return>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
-
-
-
def onEvent(self, event, cellName, cellEntry):
-
""" define events, evaluate and show cell
-
"""
-
# you can get red if cellEntry and get the Entry from the event.
-
thisEntry = event.widget
-
if event.num == 1:
-
#should retreive equation from dictionary then print it
-
equation = self.entryValueDict[cellName]
-
# need to clear the Entry here
-
thisEntry.insert(0, equation)
-
-
def returnKey(self, event, cellName, cellEntry):
-
"""
-
define returnKey event
-
"""
-
"""
-
self.entriesDict[cellName] = cellEntry.get()
-
try:
-
result = eval(data, self.entriesDict)
-
cellData.delete(0, 'end') #delete all text in the widget
-
cellData.insert(0, str(result)) #replace with result
-
except:
-
pass #nothing should crash the spreadsheet
-
"""
-
self.entriesDict[cellName] = cellEntry.get()
-
dictNew={}
-
for keys in self.entriesDict:
-
expr = self.entriesDict[keys]
-
#evaluate expr using dictNew
-
result=eval(expr, self.entriesDict)
-
#put result in dictNew under key
-
cellData.insert(0, str(result))
-
-
-
-
-
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()
-
I hope this gets you going in the right direction: - from Tkinter import *
-
class spreadsheet(Frame):
-
"""
-
initialize columns and rows, default of 5
-
"""
-
def __init__(self, parent=None, nr=4, nc=4):
-
Frame.__init__(self, parent)
-
self.numrow = nr
-
self.numcol = nc
-
self.entryValueDict = {} # an empty dictionary
-
self.entryFormulaDict = {} # an empty dictionary
-
self.makeWidgets(nr, nc)
-
-
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
-
cellEntry = Entry(root, width=w)
-
cellEntry.grid(row=row, column=col)
-
cellName = "%s%s" %(rowLabel[row], col-1)
-
self.entryValueDict[cellName] = cellName # just testing; should be ""
-
self.entryFormulaDict[cellName] = ""
-
# bind to left mouse click
-
cellEntry.bind('<Button-1>', lambda e, cellName=cellName: self.onEvent(e, cellName, cellEntry))
-
# bind the object to a right mouse click
-
cellEntry.bind('<Tab>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
-
# bind the object to a return/enter press
-
cellEntry.bind('<Return>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
-
-
-
def onEvent(self, event, cellName, cellEntry):
-
""" define events, evaluate and show cell
-
"""
-
# you can get red if cellEntry and get the Entry from the event.
-
thisEntry = event.widget
-
if event.num == 1:
-
#should retreive equation from dictionary then print it
-
equation = self.entryValueDict[cellName]
-
# need to clear the Entry here
-
thisEntry.insert(0, equation)
-
-
def returnKey(self, event, cellName, cellEntry):
-
"""
-
define returnKey event
-
"""
-
"""
-
self.entriesDict[cellName] = cellEntry.get()
-
try:
-
result = eval(data, self.entriesDict)
-
cellData.delete(0, 'end') #delete all text in the widget
-
cellData.insert(0, str(result)) #replace with result
-
except:
-
pass #nothing should crash the spreadsheet
-
"""
-
self.entriesDict[cellName] = cellEntry.get()
-
dictNew={}
-
for keys in self.entriesDict:
-
expr = self.entriesDict[keys]
-
#evaluate expr using dictNew
-
result=eval(expr, self.entriesDict)
-
#put result in dictNew under key
-
cellData.insert(0, str(result))
-
-
-
-
-
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()
-
This might fix the other problem of always insterting into the last cell: - # bind to left mouse click
-
cellEntry.bind('<Button-1>', lambda e, cellName=cellName, cellEntry=cellEntry: self.onEvent(e, cellName, cellEntry))
-
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Rob T |
last post by:
I have a routine that imports a list of part numbers into a dataview:
strExcel = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
boxFile.Text & ";Extended Properties=""Excel 8.0;HDR=NO"""...
|
by: Paulers |
last post by:
Is there an event that gets triggered when the spreadsheet control
loads? I am trying to modify my column headers so when the spreadsheet
loads, the column headers are displayed.
thanks
|
by: Joseph Geretz |
last post by:
Writing an Outlook AddIn with C#. For the user interface within Outlook I'm
adding matching pairs of Toolbar buttons and Menu items. All of the buttons
and menu items are wired up to send events to...
|
by: Ian |
last post by:
Hi everyone,
I have found some bizarre (to me...!) behaviour of the Form_Activate
function. I have a form which has a button control used to close the
form and a subform with a datasheet view...
|
by: toffee |
last post by:
Hi all,
I created a little button which when clicked will run a query on mysql db
and output the results as an excel spreadsheet.
I do this by setting the header as application excel. All works...
|
by: See_Red_Run |
last post by:
I am working with a database that isn't automated ie no switchboard or
similar device was made. I've pretty much gotten everything added to
the
switchboard except for two related key parts.
...
|
by: TMS |
last post by:
Only a few weeks of school left. This assignment and then a project to go (GASP).
I'm writing a spreadsheet. I am required to have each 'cell' do 2 things: Evaluate if it is an equation, or...
|
by: oliver james |
last post by:
I'm trying to automate an Excel spreadsheet from Access. I've
established a link and loaded data from a recordset onto a worksheet
in Excel. I now want to perform some manipulation on the data. I...
|
by: jit007 |
last post by:
HI!!
I'm using OWC11 spreadsheet control in a VBA application. I want to do two things as described below:--
1. I want to track the KEYDOWN event when user have selected any cell in...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |