Спасибо @BryanOakley за полезную ссылку
Мой вариант решения проблемы следующий
(если кто-то может сделать это лучше, пожалуйста, ответьте)
import Tkinter as tk
import tkFont
import sys
import os
import bisect
LONG_ARRAY = ''
class KeyList(object):
# bisect doesn't accept a key function, so we build the key into our sequence.
def __init__(self, l, key):
self.l = l
self.key = key
def __len__(self):
return len(self.l)
def __getitem__(self, index):
return self.key(self.l[index], index)
class WrappingLabel(tk.Label):
'''a type of Label that automatically adjusts the wrap to the size'''
def __init__(self, master=None, **kwargs):
tk.Label.__init__(self, master, **kwargs)
self.bind('<Configure>', self.fit)
if (not hasattr(self, "original_text")):
# preserve the original text so we can restore it if the widget grows.
self.original_text = self["text"]
self.font = tkFont.nametofont(self["font"])
self.font_height = 48.88 # self.font.metrics('linespace')
def fit(self, event):
max_width = event.width
max_height = event.height / self.font_height # rough n_lines
text = LONG_ARRAY[:2000] # TODO !!! self.original_text
actual_width = self.font.measure(text)
if (actual_width > max_width * max_height):
# the original text won't fit. Keep shrinking until it does
i = bisect.bisect_left(KeyList(text, key=lambda x,i: self.font.measure(text[:i]+ '...')), max_width * max_height)
lb3_text.set(text[:i] + '...') # TODO !!! self.original_text
return self.config(wraplength=self.winfo_width())
if __name__ == '__main__':
root = tk.Tk()
root.geometry("1280x640")
dFont=tkFont.Font(family="Arial", size=30) # fixed Font
LONG_ARRAY = 'a' * 100000 # of symbols
root.grid_columnconfigure(0, weight=1)
frame = tk.Frame(root)
tk.Grid.rowconfigure(root, 0, weight=1)
tk.Grid.columnconfigure(root, 0, weight=1)
frame.grid(row=0, column=0, sticky=tk.NSEW)
tb_date = tk.Entry(frame, font=dFont)
tb_date.grid(column=0, row=0, columnspan=3, sticky=tk.NSEW)
bt_find = tk.Button(frame, text="...", font=dFont)
bt_find.grid(column=9, row=0, columnspan=2, sticky=tk.NSEW)
lb1_text = tk.StringVar()
lb1_text.set("1")
lb1 = tk.Label(frame, textvariable=lb1_text, width=10, font=dFont, anchor=tk.NW)
lb1.grid(column=0, row=1, sticky=tk.NSEW)
lb3_text = tk.StringVar()
lb3 = WrappingLabel(frame, textvariable=lb3_text, font=dFont, anchor=tk.NW, justify=tk.LEFT)
#lb3 = tk.Text(frame, font=dFont, state=tk.DISABLED, wrap=tk.CHAR) # bg=frame["bg"], fg='black',
lb3.grid(column=1, row=1, rowspan=2, columnspan=9, sticky=tk.NSEW)
lb2_text = tk.StringVar()
lb2 = tk.Label(frame, textvariable=lb2_text, width=10, font=dFont, anchor=tk.NW)
lb2.grid(column=1, row=4, columnspan=9, rowspan=2, sticky=tk.NSEW)
lb2_text.set('................................................')
for y in range(11):
tk.Grid.columnconfigure(frame, y, weight=1)
for x in range(5):
tk.Grid.rowconfigure(frame, x, weight=1)
lb3_text.set(LONG_ARRAY[:2000])
#lb3.insert(tk.INSERT, LONG_ARRAY[:2000])
#lb3.insert(tk.END, "")
root.mainloop()