Search code examples
python-3.xtkintertkinter-text

searching for tags in Tkinter Text widget


i am using a tkinter Text widget to display the content of gerber-code files. the program runs on a raspberry pi and send code over serial to a machine one line of text at at time.

i set the current active line as follows:

class TextEditor(tkinter.Text):
def __init__(self, tkRoot):
    ...
    self.tag_configure("activeLine", background="#87e8ed")# set the colour used for activeLine

def setLine(self, lineNumber):
    self.tag_remove("activeLine", "1.0", "end")
    self.tag_add("activeLine", str(lineNumber)+".0 linestart", str(lineNumber)+".0 lineend+1c")

def getLine(self):
    pass # need to return the activeLine line number

there should only ever be one line at a time highlighted with "activeLine" so the first instance would be fine.

i could store a variable in the call to setLine and read it back in getLine but i would prefer not to as any edits to the text it could go out of sink

i notice using IDLE that the debugger uses what looks the same principle as i am trying to achieve here to set breakpoints, is it possible and if so where would i start looking for the IDLE source code to look into how it is achieved there, i am currently writing this on a Ubuntu 18.04 desktop i would like to no best ways to search IDLE source from

any help would be greatly appreciated, i am quite new to python and tkinter as i am generally a windows dot.net programmer but i am now learning to use Linux


Solution

  • i have now found an answer to my own question

    listing all the functions of the text widget that start with "tag_" like this:

    d = dir(self.tkRoot.text)
    for dv in d:
        s = str(dv)
        if s.startswith("tag_"):
            print(dv)
    

    i found the method "tag_ranges(name)" that returns me this

    (<textindex object: '5.0'>, <textindex object: '6.0'>)

    at the time of calling the current line was 5