[ python-Bugs-615772 ] Tkinter.Misc has no __contains__ method

SourceForge.net noreply at sourceforge.net
Fri Mar 31 19:00:20 CEST 2006


Bugs item #615772, was opened at 2002-09-28 07:27
Message generated for change (Comment added) made by tzot
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=615772&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Tkinter
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Samuel Bronson (sbronson)
Assigned to: Nobody/Anonymous (nobody)
Summary: Tkinter.Misc has no __contains__ method

Initial Comment:
This can lead to a lot of confusion.

If I do `"spam" in widget', I get a TypeError.

Case in point: (the code in question is probably
incorrect, so I am not including it)

 /usr/tmp/python-14024DA 
   45             self.pack()
   46 
   47 if __name__ == '__main__':
   48     widget = TkLife(None, height=20, width=20)
   49     widget.loop()
widget undefined
TkLife = <class __main__.TkLife>
None undefined
height undefined
width undefined

 /usr/tmp/python-14024DA in
__init__(self=<__main__.TkLife instance>, master=None,
**kw={'height': 20, 'width': 20})
   27         self['state'] = Tkinter.DISABLED
   28 
   29         if 'cells' not in self:
   30             assert 'width' in self and 'height'
in self
   31             self['cells'] =
randomcells(self['width'], self['height'])
self = <__main__.TkLife instance>

 /usr/local/lib/python2.2/lib-tk/Tkinter.py in
cget(self=<__main__.TkLife instance>, key=0)
 1088     def cget(self, key):
 1089         """Return the resource value for a KEY
given as string."""
 1090         return self.tk.call(self._w, 'cget', '-'
+ key)
 1091     __getitem__ = cget
 1092     def __setitem__(self, key, value):
self = <__main__.TkLife instance>
self.tk = <tkapp object>
self.tk.call = <built-in method call of tkapp object>
self._w = '.135644212'
key = 0
TypeError: cannot concatenate 'str' and 'int' objects
      __doc__ =
'Inappropriate argument type.'
      __getitem__ =
<bound method TypeError.__getitem__ of
<exceptions.TypeError instance at 0x83202cc>>
      __init__ =
<bound method TypeError.__init__ of
<exceptions.TypeError instance at 0x83202cc>>
      __module__ =
'exceptions'
      __str__ =
<bound method TypeError.__str__ of
<exceptions.TypeError instance at 0x83202cc>>
      args =
("cannot concatenate 'str' and 'int' objects",)

The above is a description of an error in a Python
program.  Here is
the original traceback:

Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "/usr/tmp/python-14024DA", line 48, in ?
    widget = TkLife(None, height=20, width=20)
  File "/usr/tmp/python-14024DA", line 29, in __init__
    if 'cells' not in self:
  File "/usr/local/lib/python2.2/lib-tk/Tkinter.py",
line 1090, in cget
    return self.tk.call(self._w, 'cget', '-' + key)
TypeError: cannot concatenate 'str' and 'int' objects


----------------------------------------------------------------------

Comment By: Christos Georgiou (tzot)
Date: 2006-03-31 20:00

Message:
Logged In: YES 
user_id=539787

The __setitem__ and __getitem__ methods are there only as a
convenience, not to emulate dictionaries.  The TclError
returned ("unknown option...") is clear enough to be handled
in an except clause.

I vote -0 for inclusion of such functionality (`option in
widget`) for LBYL code.

----------------------------------------------------------------------

Comment By: Jeff Epler (jepler)
Date: 2002-09-29 16:52

Message:
Logged In: YES 
user_id=2772

This patch makes Tkinter widgets more "dict-like".  I don't
know if this is really desirable, I've never configured
widgets through the __setitem__ interface.

* __getitem__ raises KeyError when cget() raises TclError
* __setitem__ raises KeyError when configure() raises TclError
* implemented has_key(), __contains__(), values(), items()

Uh, I'm obviously blind here, because I don't see the
"attach file" section of the tracker.  Sorry, but I'll have
to inline the patch.  I'll also place it at
http://unpythonic.net/~jepler/tkinter-dictlike.diff

--- Tkinter.py.ori      Sun Sep 29 08:30:58 2002
+++ Tkinter.py  Sun Sep 29 08:46:20 2002
@@ -1096,13 +1096,27 @@
     def cget(self, key):
         """Return the resource value for a KEY given as
string."""
         return self.tk.call(self._w, 'cget', '-' + key)
-    __getitem__ = cget
+    def __getitem__(self, key):
+        try:
+            return self.cget(key)
+        except TclError:
+            raise KeyError, key
     def __setitem__(self, key, value):
-        self.configure({key: value})
+        try:
+            self.configure({key: value})
+        except TclError:
+            raise KeyError, key
+    def has_key(self, key):
+        return key in self.keys()
+    __contains__ = has_key
     def keys(self):
         """Return a list of all resource names of this
widget."""
         return map(lambda x: x[0][1:],
                self.tk.split(self.tk.call(self._w,
'configure')))
+    def values(self):
+        return [self[k] for k in self.keys()]
+    def items(self):
+        return [(k, self[k]) for k in self.keys()]
     def __str__(self):
         """Return the window path name of this widget."""
         return self._w


----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=615772&group_id=5470


More information about the Python-bugs-list mailing list