[Python-checkins] python/dist/src/Lib/lib-tk Tkinter.py,1.161,1.162

gvanrossum@sourceforge.net gvanrossum@sourceforge.net
Tue, 23 Apr 2002 06:27:27 -0700


Update of /cvsroot/python/python/dist/src/Lib/lib-tk
In directory usw-pr-cvs1:/tmp/cvs-serv20509

Modified Files:
	Tkinter.py 
Log Message:
SF patch 546244 by John Williams: add Text.dump() method.


Index: Tkinter.py
===================================================================
RCS file: /cvsroot/python/python/dist/src/Lib/lib-tk/Tkinter.py,v
retrieving revision 1.161
retrieving revision 1.162
diff -C2 -d -r1.161 -r1.162
*** Tkinter.py	27 Mar 2002 17:15:57 -0000	1.161
--- Tkinter.py	23 Apr 2002 13:27:24 -0000	1.162
***************
*** 2628,2632 ****
  class Text(Widget):
      """Text widget which can display text in various forms."""
-     # XXX Add dump()
      def __init__(self, master=None, cnf={}, **kw):
          """Construct a text widget with the parent MASTER.
--- 2628,2631 ----
***************
*** 2672,2675 ****
--- 2671,2712 ----
          the character at INDEX."""
          return self._getints(self.tk.call(self._w, 'dlineinfo', index))
+     def dump(self, index1, index2=None, command=None, **kw):
+         """Return the contents of the widget between index1 and index2.
+         
+         The type of contents returned in filtered based on the keyword
+         parameters; if 'all', 'image', 'mark', 'tag', 'text', or 'window' are
+         given and true, then the corresponding items are returned. The result
+         is a list of triples of the form (key, value, index). If none of the
+         keywords are true then 'all' is used by default.
+         
+         If the 'command' argument is given, it is called once for each element
+         of the list of triples, with the values of each triple serving as the
+         arguments to the function. In this case the list is not returned."""
+         args = []
+         func_name = None
+         result = None
+         if not command:
+             # Never call the dump command without the -command flag, since the
+             # output could involve Tcl quoting and would be a pain to parse
+             # right. Instead just set the command to build a list of triples
+             # as if we had done the parsing.
+             result = []
+             def append_triple(key, value, index, result=result):
+                 result.append((key, value, index))
+             command = append_triple
+         try:
+             if not isinstance(command, str):
+                 func_name = command = self._register(command)
+             args += ["-command", command]
+             for key in kw:
+                 if kw[key]: args.append("-" + key)
+             args.append(index1)
+             if index2:
+                 args.append(index2)
+             self.tk.call(self._w, "dump", *args)
+             return result
+         finally:
+             if func_name:
+                 self.deletecommand(func_name)
      def get(self, index1, index2=None):
          """Return the text from INDEX1 to INDEX2 (not included)."""