467,923 Members | 1,449 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,923 developers. It's quick & easy.

strange 'event' in spreadsheet

TMS
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
  • viewed: 1470
Share:
2 Replies
bartonc
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
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

Post your reply

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

Similar topics

5 posts views Thread by Rob T | last post: by
4 posts views Thread by toffee | last post: by
8 posts views Thread by See_Red_Run | last post: by
TMS
3 posts views Thread by TMS | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.