473,324 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

strange 'event' in spreadsheet

TMS
119 100+
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!!!!!

Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. class spreadsheet(Frame):
  3.     """
  4.     initialize columns and rows, default of 5
  5.     """
  6.     def __init__(self, parent=None, nr=4, nc=4):
  7.         Frame.__init__(self, parent)
  8.         self.numrow = nr
  9.         self.numcol = nc
  10.         self.entriesDict = {}    # an empty dictionary
  11.         self.makeWidgets(nr, nc) 
  12.     def onEvent(self, event, cellName, cellEntry):
  13.         """ define events, evaluate and show cell
  14.         """
  15.         if event.num == 1:  
  16.             #should retreive equation from dictionary then print it
  17.             equation = self.entriesDict[cellName]
  18.             cellEntry.insert(0, equation)
  19.  
  20.     def returnKey(self, event, cellName, cellEntry):
  21.         """
  22.         define returnKey event
  23.         """
  24.         """
  25.         self.entriesDict[cellName] = cellEntry.get()
  26.         try:
  27.             result = eval(data, self.entriesDict)
  28.             cellData.delete(0, 'end')  #delete all text in the widget
  29.             cellData.insert(0, str(result))  #replace with result
  30.         except:
  31.             pass  #nothing should crash the spreadsheet
  32.             """
  33.         self.entriesDict[cellName] = cellEntry.get()
  34.         dictNew={}
  35.         for keys in self.entriesDict:
  36.             expr = self.entriesDict[keys]
  37.             #evaluate expr using dictNew
  38.             result=eval(expr, self.entriesDict)
  39.             #put result in dictNew under key
  40.             cellData.insert(0, str(result))
  41.     def makeWidgets(self, numrow, numcol):
  42.         """
  43.         define labels for rows and columns, use entry widget, assign events, create dictionary of
  44.         cell:widget pair
  45.         """
  46.         dict = {}
  47.         w = 20
  48.         h = 1
  49.         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']
  50.         for row in range(numrow):
  51.             for col in range(numcol):
  52.                 if col == 0:
  53.                     # label rows
  54.                     labels = Label(root, width = 3, text = rowLabel[row])
  55.                     labels.grid(row=row, column=col, padx = 2, pady = 2)
  56.                 elif row == 0:
  57.                     # label columns
  58.                     labels = Label(root, width=3, text = str(col-1))
  59.                     labels.grid(row=row, column=col, padx = 2, pady =2)
  60.                 else:
  61.                     # use entry widget
  62.                     cellEntry = Entry(root, width=w)
  63.                     cellEntry.grid(row=row, column=col)
  64.                     cellName = "%s%s" %(rowLabel[row], col-1)
  65.                     self.entriesDict[cellName] = cellEntry
  66.                     # bind to left mouse click
  67.                     cellEntry.bind('<Button-1>', lambda e, cellName=cellName: self.onEvent(e, cellName, cellEntry))
  68.                     # bind the object to a right mouse click
  69.                     cellEntry.bind('<Tab>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
  70.                     # bind the object to a return/enter press
  71.                     cellEntry.bind('<Return>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
  72.  
  73. if __name__ == '__main__':
  74.     import sys
  75.     root = Tk()
  76.     root.title('S P R E A D S H E E T')
  77.     if len(sys.argv) != 3:
  78.         spreadsheet(root).grid()
  79.     else:
  80.         rows, cols = eval(sys.argv[1]), eval(sys.argv[2])
  81.         spreadsheet(root, rows, cols).grid()
  82.     root.mainloop()
  83.  
  84.  
Apr 4 '07 #1
2 1766
bartonc
6,596 Expert 4TB
I hope this gets you going in the right direction:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. class spreadsheet(Frame):
  3.     """
  4.     initialize columns and rows, default of 5
  5.     """
  6.     def __init__(self, parent=None, nr=4, nc=4):
  7.         Frame.__init__(self, parent)
  8.         self.numrow = nr
  9.         self.numcol = nc
  10.         self.entryValueDict = {}   # an empty dictionary
  11.         self.entryFormulaDict = {}   # an empty dictionary
  12.         self.makeWidgets(nr, nc)
  13.  
  14.     def makeWidgets(self, numrow, numcol):
  15.         """
  16.         define labels for rows and columns, use entry widget, assign events, create dictionary of
  17.         cell:widget pair
  18.         """
  19.         dict = {}
  20.         w = 20
  21.         h = 1
  22.         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']
  23.         for row in range(numrow):
  24.             for col in range(numcol):
  25.                 if col == 0:
  26.                     # label rows
  27.                     labels = Label(root, width = 3, text = rowLabel[row])
  28.                     labels.grid(row=row, column=col, padx = 2, pady = 2)
  29.                 elif row == 0:
  30.                     # label columns
  31.                     labels = Label(root, width=3, text = str(col-1))
  32.                     labels.grid(row=row, column=col, padx = 2, pady =2)
  33.                 else:
  34.                     # use entry widget
  35.                     cellEntry = Entry(root, width=w)
  36.                     cellEntry.grid(row=row, column=col)
  37.                     cellName = "%s%s" %(rowLabel[row], col-1)
  38.                     self.entryValueDict[cellName] = cellName # just testing; should be ""
  39.                     self.entryFormulaDict[cellName] = ""
  40.                     # bind to left mouse click
  41.                     cellEntry.bind('<Button-1>', lambda e, cellName=cellName: self.onEvent(e, cellName, cellEntry))
  42.                     # bind the object to a right mouse click
  43.                     cellEntry.bind('<Tab>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
  44.                     # bind the object to a return/enter press
  45.                     cellEntry.bind('<Return>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
  46.  
  47.  
  48.     def onEvent(self, event, cellName, cellEntry):
  49.         """ define events, evaluate and show cell
  50.         """
  51.         # you can get red if cellEntry and get the Entry from the event.
  52.         thisEntry = event.widget
  53.         if event.num == 1:
  54.             #should retreive equation from dictionary then print it
  55.             equation = self.entryValueDict[cellName]
  56.             # need to clear the Entry here
  57.             thisEntry.insert(0, equation)
  58.  
  59.     def returnKey(self, event, cellName, cellEntry):
  60.         """
  61.         define returnKey event
  62.         """
  63.         """
  64.         self.entriesDict[cellName] = cellEntry.get()
  65.         try:
  66.                 result = eval(data, self.entriesDict)
  67.                 cellData.delete(0, 'end')  #delete all text in the widget
  68.                 cellData.insert(0, str(result))  #replace with result
  69.         except:
  70.                 pass  #nothing should crash the spreadsheet
  71.                 """
  72.         self.entriesDict[cellName] = cellEntry.get()
  73.         dictNew={}
  74.         for keys in self.entriesDict:
  75.             expr = self.entriesDict[keys]
  76.             #evaluate expr using dictNew
  77.             result=eval(expr, self.entriesDict)
  78.             #put result in dictNew under key
  79.             cellData.insert(0, str(result))
  80.  
  81.  
  82.  
  83.  
  84. if __name__ == '__main__':
  85.     import sys
  86.     root = Tk()
  87.     root.title('S P R E A D S H E E T')
  88.     if len(sys.argv) != 3:
  89.         spreadsheet(root).grid()
  90.     else:
  91.         rows, cols = eval(sys.argv[1]), eval(sys.argv[2])
  92.         spreadsheet(root, rows, cols).grid()
  93.     root.mainloop()
  94.  
Apr 4 '07 #2
bartonc
6,596 Expert 4TB
I hope this gets you going in the right direction:
Expand|Select|Wrap|Line Numbers
  1. from Tkinter import *
  2. class spreadsheet(Frame):
  3.     """
  4.     initialize columns and rows, default of 5
  5.     """
  6.     def __init__(self, parent=None, nr=4, nc=4):
  7.         Frame.__init__(self, parent)
  8.         self.numrow = nr
  9.         self.numcol = nc
  10.         self.entryValueDict = {}   # an empty dictionary
  11.         self.entryFormulaDict = {}   # an empty dictionary
  12.         self.makeWidgets(nr, nc)
  13.  
  14.     def makeWidgets(self, numrow, numcol):
  15.         """
  16.         define labels for rows and columns, use entry widget, assign events, create dictionary of
  17.         cell:widget pair
  18.         """
  19.         dict = {}
  20.         w = 20
  21.         h = 1
  22.         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']
  23.         for row in range(numrow):
  24.             for col in range(numcol):
  25.                 if col == 0:
  26.                     # label rows
  27.                     labels = Label(root, width = 3, text = rowLabel[row])
  28.                     labels.grid(row=row, column=col, padx = 2, pady = 2)
  29.                 elif row == 0:
  30.                     # label columns
  31.                     labels = Label(root, width=3, text = str(col-1))
  32.                     labels.grid(row=row, column=col, padx = 2, pady =2)
  33.                 else:
  34.                     # use entry widget
  35.                     cellEntry = Entry(root, width=w)
  36.                     cellEntry.grid(row=row, column=col)
  37.                     cellName = "%s%s" %(rowLabel[row], col-1)
  38.                     self.entryValueDict[cellName] = cellName # just testing; should be ""
  39.                     self.entryFormulaDict[cellName] = ""
  40.                     # bind to left mouse click
  41.                     cellEntry.bind('<Button-1>', lambda e, cellName=cellName: self.onEvent(e, cellName, cellEntry))
  42.                     # bind the object to a right mouse click
  43.                     cellEntry.bind('<Tab>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
  44.                     # bind the object to a return/enter press
  45.                     cellEntry.bind('<Return>', lambda e, cellName=cellName: self.returnKey(e, cellName, cellEntry))
  46.  
  47.  
  48.     def onEvent(self, event, cellName, cellEntry):
  49.         """ define events, evaluate and show cell
  50.         """
  51.         # you can get red if cellEntry and get the Entry from the event.
  52.         thisEntry = event.widget
  53.         if event.num == 1:
  54.             #should retreive equation from dictionary then print it
  55.             equation = self.entryValueDict[cellName]
  56.             # need to clear the Entry here
  57.             thisEntry.insert(0, equation)
  58.  
  59.     def returnKey(self, event, cellName, cellEntry):
  60.         """
  61.         define returnKey event
  62.         """
  63.         """
  64.         self.entriesDict[cellName] = cellEntry.get()
  65.         try:
  66.                 result = eval(data, self.entriesDict)
  67.                 cellData.delete(0, 'end')  #delete all text in the widget
  68.                 cellData.insert(0, str(result))  #replace with result
  69.         except:
  70.                 pass  #nothing should crash the spreadsheet
  71.                 """
  72.         self.entriesDict[cellName] = cellEntry.get()
  73.         dictNew={}
  74.         for keys in self.entriesDict:
  75.             expr = self.entriesDict[keys]
  76.             #evaluate expr using dictNew
  77.             result=eval(expr, self.entriesDict)
  78.             #put result in dictNew under key
  79.             cellData.insert(0, str(result))
  80.  
  81.  
  82.  
  83.  
  84. if __name__ == '__main__':
  85.     import sys
  86.     root = Tk()
  87.     root.title('S P R E A D S H E E T')
  88.     if len(sys.argv) != 3:
  89.         spreadsheet(root).grid()
  90.     else:
  91.         rows, cols = eval(sys.argv[1]), eval(sys.argv[2])
  92.         spreadsheet(root, rows, cols).grid()
  93.     root.mainloop()
  94.  
This might fix the other problem of always insterting into the last cell:
Expand|Select|Wrap|Line Numbers
  1. # bind to left mouse click
  2.                     cellEntry.bind('<Button-1>', lambda e, cellName=cellName, cellEntry=cellEntry: self.onEvent(e, cellName, cellEntry))
  3.  
Apr 4 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
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"""...
0
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
6
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...
5
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...
4
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...
8
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. ...
3
TMS
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...
4
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...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
0
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...
0
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...
1
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)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
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....
0
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
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.