On 2/3/2013 8:08 PM, Saul Spatz wrote:
To the good people on comp.lang.python:

I have the following Tkinter class (python 2.7.3):

from Tkinter import *

class ScrolledCanvas(Frame): def __init__(self, master, width,
height, bg, cursor): Frame.__init__(self, master) self.__nonzero__ =
lambda: True canv = self.canvas = Canvas(self, bg=bg, relief=SUNKEN)
# self.__getattr__ = lambda x, name: getattr(self.canvas, name)
canv.config(width=width, height=height)           # display area
size canv.config(scrollregion=(0, 0, width, height))   # canvas size
corners canv.config(highlightthickness=0)                 # no pixels
to border

ybar = Scrollbar(self) ybar.config(command=canv.yview)
# xlink sbar and canv canv.config(yscrollcommand=ybar.set)
# move one moves other

xbar = Scrollbar(self) xbar.config(command=canv.xview)
# xlink sbar and canv canv.config(xscrollcommand=xbar.set)
# move one moves other

canv.grid(row = 0, column = 0, sticky = 'news') ybar.grid(row = 0,
column = 1, sticky = 'ns') xbar.grid(row = 1, column = 0, sticky =
'ew') self.rowconfigure(0, weight = 1) self.columnconfigure(0, weight
= 1)

self.create_text(20, 20, text = 'Did it!', fill = 'red')

def __getattr__(self, name): return getattr(self.canvas, name)

root = Tk() app = ScrolledCanvas(root, 400, 300, 'white', 'hand2')
app.pack() root.mainloop()

I just added the __getattr__ method, and the program crashed in the
Canvas constructor.  There is apparently a call to self.__nonzero__
somewhere in Tkinter.py, and since the constructor hasn't exited yet,
sel.fcanvas isn't defined yet, so __getattr__ recurses indefinitely.

I fixed this as you see, by defining self.__nonzero__ before the call
to the constructor.  Now, I have  two questions:

1. I originally defined self.__nonzero__ = lambda x: True, on the
assumption that when self.__nonzero__ was called, the interpreter
would pass self as an argument.  Wrong.  No arguments were passed.
Why is this?

Because you made __nonzero__ an instance function attribute instead of an instance method class attribute as would be the case if you wrote

def __nonzero__(self): return True

outside of __init__.

2. I find this solution rather unsatisfactory, since there's a rather
obscure line of code here.  I tried eliminating the def of
__gertattr__ and the definition of self.__nonzero__ and adding this
line after the constructor:

self.__getattr__=  lambda name: getattr(self.canvas, name)

I presume __getattr__ is only looked up on the class and never on the instance, not even as a backup.

This get through the constructor all right, but crashes with the
message that a ScrolledCanvas object has no create_text attribute.
(I've tried passing two arguments to the lambda, but it makes no
difference.)

If you are just starting out, consider 3.3 unless you really have to use 2.7.

--
Terry Jan Reedy

--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to