The "real" name

James Stroud jstroud at ucla.edu
Sat Jan 21 17:23:49 EST 2006


engsolnorm at hotmail.com wrote:
> I'm playing with a sudoku GUI...just to learn more about python.
> 
> I've made 81 'cells'...actually small canvases
> 
> Part of my scheme to write the cells (all 81 of them in the gui) to a file (using the the SAVE callback/button), then
> restore the gui cells from the contents of the saved file, which depends on knowing the "name" of the cell with the
> focus, or one (or more) which have a number.
> 
> The print shows .9919624.9990312, but this nunber (name?) does not work in:
> 
> cell-name of cell-.create_text(18,18, text = somevar, fill = 'blue' , font = ('arial', 18, 'bold'))
> 
> Also, how can I declare a variable outside of the mainloop/callback scheme which would be 'known' to the callbacks?
> 
> Thanks,
> Norm
> 

I guess you are using tkinter.

".9919624.9990312" in tkinter is just a string representation of the 
underlying object, in this case a Canvas(). It is not up to a python 
programmer to understand exactly what these numbers are. They are used 
by Tcl/Tk internally.

Tk objects are not pickleable. Better is to create a datastructure that 
can be pickled from info gleaned specifically with the itemcget() 
method. Example code is below. See the Pickle/cPickle documentation. 
They are very easy to use.

Since you haven't posted any code, I can only guess what you are doing. 
But you may want to try variations on the following (read the comments):

from Tkinter import *

# This is how you may want to make a bunch of canvases in a grid.
def make_canvases(parent, rows=9, cols=9, **options):
   """
   Pass in rows, cols, and any options the canvases should
   require.
   """
   cells = []
   for i in xrange(rows):
     arow = []
     for j in xrange(cols):
       c = Canvas(parent, **options)
       c.grid(row=i, column=j)
       arow.append(c)
     cells.append(arow)
   return cells

def demo():
   """
   Tests out our make_canvases() function.
   """

   # tkinter stuff--setting up
   tk = Tk()
   f = Frame(tk)
   f.pack(expand=YES, fill=BOTH)

   # make the canvases the gui-programmer way
   canvases = make_canvases(f, height=25, width=25)

   # individual access to canvases (remember that indices start at 0)
   canvases[0][0].configure(background='orange')
   canvases[7][8].create_text(14, 8, text='Bob',
                              fill='blue',
                              font=('arial', 14, 'bold'))
   canvases[8][8].create_text(14, 8, text='9,9',
                              fill='blue',
                              font=('arial', 14, 'bold'))

   # accessing inside loops
   for i in xrange(9):
     canvases[i][8-i].configure(background='red')

   # fun with bindings (see your last question)
   # you should study this one!
   for i in xrange(9):
     for j in xrange(9):
       c = canvases[i][j]
       c.bind("<Button-1>",
          lambda e=None, c=c: c.configure(background='green'))

   # getting out info
   texts = []
   for i in xrange(9):
     for j in xrange(9):
       c = canvases[i][j]
       for t in c.find_all():
         try:
           text = c.itemcget(t, 'text')
           texts.append((i,j,text))
         except:
           pass

   # reporting the got-out info
   Label(tk, text="Texts are: %s" % texts).pack(expand=YES, fill=X)
   tk.mainloop()

demo()



More information about the Python-list mailing list