[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()
logger.setLevel(logging.DEBUG)
stream_handler = logging.StreamHandler()
stream_handler.setLevel(logging.DEBUG)
logger.addHandler(stream_handler)

def parse_dump(self, dump):
     """Parse the list, created by tkinter text dump()"""
     keystack = []
     output = []
     for section in dump:
         logger.debug("{0}".format(section))
         key, tag, index = section
         if key == 'tagon':
             keystack.insert(0, tag)
             logger.debug("Insert Tag: {}".format(tag))
         elif key == 'tagoff':
             try:
                 keystack.pop(keystack.index(tag))
                 logger.debug("Pop Tag: {}".format(tag))
             except ValueError as e:
                 pass #!!!!
         elif key in ('window', 'image', 'mark'):
             pass
         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])

Ingo



More information about the Tkinter-discuss mailing list