[Tkinter-discuss] removing tags from tkinter.Text

Martin B. spooky.ln at tbs-software.com
Fri Feb 4 17:21:49 CET 2011


Hi all,
I'm trying some code snippets with tags in Text.

In example i only hilite line on cursor.insert.

1. first tag removing is only from visible lines
2. second removing from whole buffer
all this using -after command

3. using -bind to <Key> event

3 is unusable for me coz highlighted line is always after cursor if i
move UP and DOWN

2 is better but tag is applied on last line too if i move UP after.
But still better than 3

1 on first i dont want to remove tags from whole widget but after some
timing this way is faster than 2. [ tested on 120kb .py file]

any ideas for improvement and some speedup for this ?
thanks.

##########################################################################

#encoding: utf-8
from tkinter import *
import time

class MyText(Text):
    def __init__(self, parent, **kwargs):
        super().__init__(parent, **kwargs)
        self.focus()
        self.update = None
        #self.bind('<Key>', self.hilite)
        
        self.tag_configure('cyanline', background='cyan')
        
        if self.update is None:
            self.hilite()
        
    def hilite(self, event=None):
        start = time.time()
        cur_line = int(self.index('insert').split('.')[0])
        
        #self.tag_remove('cyanline', 1.0, 'end')
        self.tag_remove('cyanline', '@0,0', '@0,{0}'.format(self.winfo_height()))
        self.tag_add('cyanline', '{0}.0'.format(cur_line), '{0}.0'.format(cur_line +1))
        self.update = self.after(100, self.hilite)
        end = time.time()
        print('Time: ', (end - start) * 10000)
        #return 'break'
    

class Test(Tk):
    def __init__(self):
        super().__init__()
        
        text = MyText(self, width=40, height=10, font='courier 8 normal')
        text.pack(fill='both', expand='yes')
        
if __name__ == '__main__':
    app = Test()
    app.title('Hilite line test')
    app.mainloop()

########################################################################


More information about the Tkinter-discuss mailing list