python - How to modify Text widget in Tkinter for interlinear anotation -
i wondering if possible create textbox tkinter can handle interlinear input. need able have different lines "connected" each other, , behaviour of each line can independent. here an example. linguistic annotation program. idea if have line, say:
this example x.det be.v a.det example.n
the spacing of first line automatically adjusted line modified allow enough space each word in second line not overlap words in first line.
is there way this?
a simple way use fixed width font (such courrier) in characters same width, format pure text padding spaces.
from tkinter import * sentence = [ 'this', 'is', 'an', 'example' ] result = [ 'x.det', 'be.v', 'a.det', 'example.n' ] line_start = [0, len(sentence)] # used split sentence lines no longer line_len line_len = 20 # max characters in each line, including spaces segment_len = 0 in range(len(sentence)): s_len = len(sentence[i]) r_len = len(result[i]) # pad words (or word groups) segments of sentence , result have same width if s_len > r_len: result[i] += ' ' * s_len - r_len elif s_len < r_len: sentence[i] += ' ' * (r_len - s_len) segment_len += max(r_len, s_len) + 1 # check line length if segment_len > line_len: segment_len = 0 line_start.insert(1, i) root = tk() in range(len(line_start)-1): sentence_segment = ' '.join( sentence[line_start[i]:line_start[i+1]] ) ts = text(root, font='tkfixedfont', width = line_len, height = 1) ts.insert(end, sentence_segment) ts.pack() result_segment = ' '.join( result[line_start[i]:line_start[i+1]] ) tr = text(root, font='tkfixedfont', width = line_len, height = 1, foreground='grey') tr.insert(end, result_segment) tr.pack() root.mainloop()
Comments
Post a Comment