[Tkinter-discuss] Transferring tags from one Text() to another

ingo ingoogni at gmail.com
Sun Feb 14 17:13:57 EST 2016

Op 2016-02-11 om 00:30 schreef Bob Greschke:
> I write stuff to a Text() and some words and lines are different colors or are links (binds) using Tags. I can Text().get(0.0, END), get all of the text, then insert that in another Text() to make a copy, but how do I get/transfer the other "formatting" info? The binds aren't real important, but the different colored text would be nice.

Not fully fleshed out, just an idea, I Text().dump() the selection and 
change the list so it can be reinserted easily. Currently not dealing 
with marks images etc.

logger = logging.getLogger()
stream_handler = logging.StreamHandler()

def parse_dump(self, dump):
     """Parse the list, created by tkinter text dump()"""
     keystack = []
     output = []
     for section in dump:
         key, tag, index = section
         if key == 'tagon':
             keystack.insert(0, tag)
             logger.debug("Insert Tag: {}".format(tag))
         elif key == 'tagoff':
                 logger.debug("Pop Tag: {}".format(tag))
             except ValueError as e:
                 pass #!!!!
         elif key in ('window', 'image', 'mark'):
         else: #== 'text'
             output.append((tag, tuple(keystack)))
         logger.debug("KeyStack: {}".format(keystack))
     return output

def insert_dump(self, pdump):
     """Insert the parsed text dump at given position"""
     for line in pdump:
         logger.debug("{} |{}".format(line[0],line[1]))
         self.insert('end', line[0], line[1])


More information about the Tkinter-discuss mailing list