473,320 Members | 1,817 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,320 software developers and data experts.

Tkinter cell has 2 strings

TMS
119 100+
This spreadsheet is almost done, but there is some functionality that is driving me nuts.

For instance: a cell, for instance 'a0' is to have 'a0' as a string, but if something is entered like '4+5', that is also there. So, at any time, one could see the cell number a0 or they could click on it and have the equation show as well. Right now, I can make it show the equation, and evaluate it, or it can also show the cell number. But I can't make it so if the user goes back to that cell after showing the cell number that the equation will show. It will always show the cell number based on the event, but it retains the part that was in there because I don't want to 'delete' it.

How do I save the equation without showing it all the time? I'm thinking it should be saved to the dictionary I already have, but alas I'm stumped. Any ideas?

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, numrow=5, numcol=5):
  7.         Frame.__init__(self, parent)
  8.         self.numrow = numrow
  9.         self.numcol = numcol
  10.         self.entriesDict = {}    # an empty dictionary
  11.         self.makeWidgets(numrow, numcol)
  12.     def onEvent(self, event, cell):
  13.         """ define events, evaluate and show cell
  14.         """
  15.         if event.num == 1:
  16.             data = self.entriesDict[cell]
  17.             data.insert(0, str(cell))  
  18.         if event.num == 3:
  19.             obj = self.entriesDict[cell]   
  20.             data = obj.get()
  21.     def returnKey(self, event, cell):
  22.         """
  23.         define returnKey event
  24.         """
  25.         obj = self.entriesDict[cell]   
  26.         data = obj.get()
  27.         try:
  28.             result = eval(data)
  29.             obj.delete(0, 'end')
  30.             obj.insert(0, str(result))
  31.         except:
  32.             pass  #nothing should crash the spreadsheet
  33.  
  34.     def makeWidgets(self, numrow, numcol):
  35.         """
  36.         define labels for rows and columns, use entry widget, assign events, create dictionary of
  37.         cell:widget pair
  38.         """
  39.         dict = {}
  40.         w = 20
  41.         h = 1
  42.         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']
  43.         for row in range(numrow):
  44.             for col in range(numcol):
  45.                 if col == 0:
  46.                     # label rows
  47.                     labels = Label(root, width = 3, text = rowLabel[row])
  48.                     labels.grid(row=row, column=col, padx = 2, pady = 2)
  49.                 elif row == 0:
  50.                     # label columns
  51.                     labels = Label(root, width=3, text = str(col-1))
  52.                     labels.grid(row=row, column=col, padx = 2, pady =2)
  53.                 else:
  54.                     # use entry widget
  55.                     entrys = Entry(root, width=w)
  56.                     entrys.grid(row=row, column=col)
  57.                     cell = "%s%s" %(rowLabel[col], row)
  58.                     self.entriesDict[cell] = entrys
  59.                     # bind to left mouse click
  60.                     entrys.bind('<Button-1>', lambda e, cell=cell: self.onEvent(e, cell))
  61.                     # bind the object to a right mouse click
  62.                     entrys.bind('<Button-3>', lambda e, cell=cell: self.onEvent(e, cell))
  63.                     # bind the object to a return/enter press
  64.                     entrys.bind('<Return>', lambda e, cell=cell: self.returnKey(e, cell))
  65.         # start curser in row a column 0
  66.         self.entriesDict['a1'].focus()
  67. if __name__ == '__main__':
  68.     import sys
  69.     root = Tk()
  70.     root.title('S P R E A D S H E E T')
  71.     if len(sys.argv) != 3:
  72.         spreadsheet(root).grid()
  73.     else:
  74.         rows, cols = eval(sys.argv[1]), eval(sys.argv[2])
  75.         spreadsheet(root, rows, cols).grid()
  76.     root.mainloop() 
  77.  
  78.  
  79.  
Mar 28 '07 #1
6 2004
bartonc
6,596 Expert 4TB
This spreadsheet is almost done, but there is some functionality that is driving me nuts.

For instance: a cell, for instance 'a0' is to have 'a0' as a string, but if something is entered like '4+5', that is also there. So, at any time, one could see the cell number a0 or they could click on it and have the equation show as well. Right now, I can make it show the equation, and evaluate it, or it can also show the cell number. But I can't make it so if the user goes back to that cell after showing the cell number that the equation will show. It will always show the cell number based on the event, but it retains the part that was in there because I don't want to 'delete' it.

How do I save the equation without showing it all the time? I'm thinking it should be saved to the dictionary I already have, but alas I'm stumped. Any ideas?
Now is the time to subclass the Entry widget to have an entryName and a value attribute. The widget (your subclass), itself would stort the value on an event (hint: bind to focusOut events as well as returnKey and enterKey events). Other events would then display either the (what do you call it? cell) name or the value.
Mar 28 '07 #2
TMS
119 100+
I've been going over your reply for a couple of days. I'm asking for some clarification.

When you say subclass the Entry widget what do you mean? I don't dare use focusOut because my teacher is very, er, uh, rigid about his requirements. Here is the paragraph that describes the 2 strings:

"These cells allow the user to type in any Python expression, which will be maintained as a string associated with the cell. In fact, the suggested implementation is to give each cell two strings: an expression and a value. The expression is what the user types into the cell. The value is a string representation of the result of evaluating the expression and is what the users sees when they are not editing a cell. When the user finishes editing the cell (usually by pressing Return or TAb), that string will be evaluated (with eval()), converted to a string, and the result will be displayed in the cell.

Initially all cells start witha value of "", and empty stirng, so all cells initially appear blank. Clicking on a cell with a mouse button brings up the cell's expression, which the user may then modify and reenter with Return or Tab. When eval() is called, the cell string may contain any Python expression, string, integer, or float. It may also contain variables with correspond to the names of defined cells (e.g. A0, C17). In addition the Python math library should be made available. Nothing should crash the spreadsheet."

ok.. so in his words, that is what is required (he actually left something out, but I'm not there yet).

I'm thinking that I need to do something like make a new variable in the __init__ function, but I'm not sure how to initialize it. I think that is because I don't understand exactly how its being stored in the dictionary. I've tried to print the dictionary to better understand it, but I can't figure out how to get it to print.

So, I guess what I'm asking is this: How do I print the dictionary so I can see exactly how the data is being stored?
I assume the equation goes out of scope when return key is pressed. So, I need to make a variable in the __init__ file, but then I'm not sure how to access it, the syntax.

I'm liking the fact that you aren't giving me the answer, and appreciate the discussion so I can understand what is going on better. I seem to get to a certain point on the assignments, then I get stuck. I kinda think its the same type of problem I get stuck on each time and its just something I don't understand clearly yet. Thank you for your help.

TMS
Mar 31 '07 #3
bvdet
2,851 Expert Mod 2GB
....
So, I guess what I'm asking is this: How do I print the dictionary so I can see exactly how the data is being stored?
.....
Hello TMS,

To print a class instance dictionary:
Expand|Select|Wrap|Line Numbers
  1. for key, value in cls_instance.__dict__.items():
  2.     print '%s = %s' % (key, value)
Mar 31 '07 #4
TMS
119 100+
That isn't the dictionary that I was referring to. I initialize a dictionary, and I think I'm using it to save the strings, both equations and results. I want to see that dictionary. See the code above.


TMS
Mar 31 '07 #5
bvdet
2,851 Expert Mod 2GB
That isn't the dictionary that I was referring to. I initialize a dictionary, and I think I'm using it to save the strings, both equations and results. I want to see that dictionary. See the code above.


TMS
Expand|Select|Wrap|Line Numbers
  1. >>> root = Tk()
  2. >>> a = spreadsheet(root)
  3. >>> print a.entriesDict
  4. {'b4': <Tkinter.Entry instance at 0x010F3A80>, 'd4': <Tkinter.Entry instance at 0x010F3F30>, 'a4': <Tkinter.Entry instance at 0x010F3800>, 'a1': <Tkinter.Entry instance at 0x010EB878>, 'a3': <Tkinter.Entry instance at 0x010EF170>, 'a2': <Tkinter.Entry instance at 0x010EF9B8>, 'b1': <Tkinter.Entry instance at 0x010EBAD0>, 'b2': <Tkinter.Entry instance at 0x010EF300>, 'b3': <Tkinter.Entry instance at 0x010EFDF0>, 'c3': <Tkinter.Entry instance at 0x010F3CB0>, 'c2': <Tkinter.Entry instance at 0x010EF2B0>, 'c1': <Tkinter.Entry instance at 0x010EF8A0>, 'c4': <Tkinter.Entry instance at 0x010F3BC0>, 'd2': <Tkinter.Entry instance at 0x010EFF58>, 'd3': <Tkinter.Entry instance at 0x010F3B20>, 'd1': <Tkinter.Entry instance at 0x010EF850>}
  5. >>> print a.entriesDict['b4']
  6. .17775232
  7. >>> 
Mar 31 '07 #6
TMS
119 100+
OK, now I see what the problem is. I am assigning an entry widget as the value to the cell. I don't want to do that, but I don't know how else to write this.

What I do want is to assign the equation as the value to the cell name. Actually, whatever string is put in should be the value, not the entry widget itself. How do I do that?

I think this is where I assign the entry widget as the value of the cell, am I wrong?

Expand|Select|Wrap|Line Numbers
  1. self.entriesDict[cell] = entrys
  2.  
I was thinking that I would make a new variable in the __init__ function so it would carry throughout the class, like data or something like that. But when I tried it didn't work.
Apr 2 '07 #7

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

Similar topics

6
by: Gary Richardson | last post by:
Is there a simple way of causing the size of a canvas to change as the window is resized when using the Grid geometry manager? Setting sticky='NESW', as in the following code, doesn't work though...
2
by: Thomas | last post by:
I was used to pass Unicode strings to Tk widgets. IIRC, Tcl/Tk expects UTF-8 encoded strings, but Tkinter took care of that. This worked, as long as I was using Python/Tk on Red Hat 9 Linux (and...
5
by: max(01)* | last post by:
hello. the following code: 1 from Tkinter import * 2 3 class MiaApp: 4 def __init__(self, genitore): 5 self.mioGenitore = genitore 6 self.i = IntVar()
6
by: William Gill | last post by:
I am trying to get & set the properties of a widget's parent widget. What I have works, but seems like a long way around the block. First I get the widget name using w.winfo_parent(), then i...
5
by: Sean McIlroy | last post by:
hi all i recently wrote a script that implements a puzzle. the interface mostly consists of a bunch of colored disks on a tkinter canvas. the problem is that the disks change their colors in...
4
by: Kevin Walzer | last post by:
I'm trying to manage user preferences in a Tkinter application by initializing some values that can then be configured from a GUI. The values are set up as a dict, like so: self.prefs= {...
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...
0
by: lee.walczak | last post by:
Hi, I hope someone can help here. I have made lots of progress implementing a gui application which at present uses the TableList python wrapper to interface with Tcl TableList implemention. ...
0
by: lee.walczak | last post by:
I actually post a topic relating to my problem here: (http://groups.google.co.uk/group/comp.lang.python/browse_thread/ thread/a073d532c4481bfe?hl=en# ) But I thought it could be useful to...
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...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
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...
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
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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.