473,544 Members | 2,517 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

strange 'event' in spreadsheet

TMS
119 New Member
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 1783
bartonc
6,596 Recognized Expert Expert
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 Recognized Expert Expert
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
2820
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""" cnnExcel = New OleDbConnection(strExcel) da = New OleDbDataAdapter("Select * from ", cnnExcel) da.Fill(ds, "Excel") dv = New...
0
979
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
2254
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 the same method (aka delegate?). I use the Tag property within this method to determine what user action is taking place. Very simple: When...
5
3101
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 showing a list of jobs from the database. When the main form loses focus and the user clicks the 'Close' button, I kept receiving error 2585 (This...
4
2012
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 well except for a very strange problem. Let's say a column should say 16500.22. When I run it here from the uk, the cell will show 16500.22 When...
8
1657
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. Right now, what I do is open a table called tbl-Temp, then use the import feature to point to an Excel spreadsheet. I select the area to be imported...
3
1659
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 if its a string just print it. Also, it is supposed to print the cell number on a different event reuqest. Right now, I'm trying to get it to...
4
4296
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 previously set up a command button in the spreadsheet to run the VBA to do this manipulation; this VBA is stored in the button's Click event. Now...
0
1234
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 the column "D". This is because, I want to track the change in the value of the cells under the column "D". 2. I want to prevent the User from...
0
7447
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7378
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7792
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7400
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5314
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
4935
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3437
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3429
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1857
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.