This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author jepler
Recipients
Date 2002-09-29.13:52:31
SpamBayes Score
Marked as misclassified
Message-id
In-reply-to
Content
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
History
Date User Action Args
2007-08-23 14:06:00adminlinkissue615772 messages
2007-08-23 14:06:00admincreate