Text

The Text() widget manages muliline text.

Display multi-line text

The Text widget displays multi-line text and is user editable. Its size is given in lines and characters.

../_images/text1.png
"""Display tk Text."""
from tklib import *

class Demo(App):
    def __init__(self): 
        super().__init__()
        Label("Text widget", font="Arial 18")

        Label('2 lines')
        Text('Initial text...', height=2, width=50)

        Label('10 lines')
        text = Text(height=10, width=50)
        text.insert('1.0', 'Add some text at the beginning of the Text widget.')

Demo().run()

text1.py

Edit text

Text can be edited as usual. Cut, copy, paste is possible. The cursor can be placed with the mouse. Mouse and and arrow key selection are both possible.

../_images/text2.png
"""Display tk Text."""
from tklib import *

class Demo(App):
    def __init__(self): 
        super().__init__()
        Label("Text widget", font="Arial 18")

        Spinbox('width', 'App.text["width"]=self.val.get()').set(80)
        Spinbox('height', 'App.text["height"]=self.val.get()').set(4)
        
        App.text = Text('Initial text...', height=2)

Demo().run()

text2.py

Undo and redo text edit

Text has an Undo and Redo function.

../_images/text3.png
"""Undo and Redo."""
from tklib import *

class Demo(App):
    def __init__(self): 
        super().__init__()
        Label("Undo and Redo", font="Arial 18")

        App.text = Text('Initial text...', height=10, width=50)
        App.text.config(undo=True)

        Button('Selection')
        Button('Edit Undo', 'App.text.edit_undo()')
        Button('Edit Redo', 'App.text.edit_redo()')
        Inspector(App.text, height=20)

Demo().run()

text3.py

Format text with tags

Text can be formatted with tags.

../_images/text4.png
from tklib import *

str = """Up until now, we've just dealt with plain text. 
Now it's time to look at how to add special formatting, such as bold, italic, 
and much more. Tk's text widget implements these using a feature called tags.
Tags are objects associated with the text widget. 
Each tag is referred to via a name. 
Each tag can have a number of different configuration options; 
these are things like fonts, colors, etc. that will be used to format text. 
Though tags are objects having state, they don't need to be explicitly created; 
they'll be automatically created the first time the tag name is used.
"""

class Demo(App):
    def __init__(self): 
        super().__init__()
        Label("Formatting with tags", font="Arial 18")

        App.text = Text(str, height=20, width=50)
        App.text.config(undo=True)

        App.text.tag_add('highlight', '5.0', '6.0')
        App.text.tag_add('highlight', '7.0', '7.18')
        App.text.tag_configure('highlight', background='yellow', 
          font='helvetica 14 bold', relief='raised')

        App.text.tag_configure('big', font='helvetica 24 bold', foreground='red')
        App.text.tag_add('big', '8.0', '8.16')

        App.text.insert('end', 'new material ', ('big'))
        App.text.insert('end', 'is ready ', ('big', 'highlight'))
        App.text.insert('end', 'soon', ('highlight'))
    
        b = Button('Popup Menu')
        App.m = ContextMenu(b)
        Item('Item 1', 'print(1)')
        Item('Item 2', 'print(2)')
        Item('Item 3', 'print(3)')
        
        App.text.tag_bind('big', '<1>', App.m.popup)

Demo().run()

text4.py

Add custom formats

Custom formats can be added.

../_images/text5.png
# Add custom styles
from tklib import *

str = """Up until now, we've just dealt with plain text. 
Now it's time to look at how to add special formatting, such as bold, italic, 
strikethrough, background colors, font sizes, and much more. Tk's text widget 
implements these using a feature called tags.
Tags are objects associated with the text widget. 
Each tag is referred to via a name chosen by the programmer. 
"""

def highlight():
    sel = App.text.tag_ranges('sel')
    if len(sel) > 0:
        App.text.tag_add('highlight', *sel)

def big():
    sel = App.text.tag_ranges('sel')
    if len(sel) > 0:
        App.text.tag_add('big', *sel)

class Demo(App):
    def __init__(self): 
        super().__init__()
        Label("Formatting with tags", font="Arial 18")

        App.text = Text(str, height=10)
        App.text.tag_configure('highlight', background='yellow')
        App.text.tag_configure('big', font='helvetica 24')
        
        Button('Selection range', 'print(App.text.tag_ranges("sel"))')
        Button('Highlight ranges', 'print(App.text.tag_ranges("highlight"))')
        Button('Big ranges', 'print(App.text.tag_ranges("big"))')
        Button('Mark names', 'print(App.text.mark_names())')
        
        Button('Highlight', highlight)
        Button('Big', big)
        
Demo().run()

text5.py

Place widgets inside text

Widgets can be placed inside text. They move with the text.

../_images/text6.png
"""Widgets inside Text"""
from tklib import *

def hello():
    print('hello')

class Demo(App):
    def __init__(self): 
        super().__init__()
        Label("Widgets inside Text", font="Arial 18")

        App.text = Text(str, height=10, width=50)
        b = ttk.Button(App.text, text='Push me', command=hello, padding=10)
        App.text.window_create('1.0', window=b)
        
Demo().run()

text6.py

Search inside text

Text can be searched. Regular expressions can be used. The exemple below shows the search for vowels which are highlighted in yellow.

../_images/text7.png
"""Search in text."""
from tklib import *

str = """Up until now, we've just dealt with plain text. 
Now it's time to look at how to add special formatting, such as bold, italic, 
strikethrough, background colors, font sizes, and much more. Tk's text widget 
implements these using a feature called tags.
"""

def highlight():
    sel = App.text.tag_ranges('sel')
    if len(sel) > 0:
        App.text.tag_add('highlight', *sel)

def search(event=None):
    App.text.tag_remove("highlight", "1.0", "end")
    start = 1.0
    while True:
        pattern = App.re.val.get()
        pos = App.text.search(pattern, start, stopindex='end', regexp=True)
        if not pos:
            break
        print(pos)
        App.text.tag_add('highlight', pos, pos+'+1c')
        start = pos + '+1c'

class Demo(App):
    def __init__(self): 
        super().__init__()
        Label("Search with regexp", font="Arial 18")

        App.text = Text(str, height=10, width=50)
        App.text.tag_configure('highlight', background='yellow')

        App.re = Entry('search', search)
             
Demo().run()

text7.py

Show a help text

Python being introspective it is easy to add help functionality.

../_images/text8.png
"""Help browser."""
from tklib import *

class Demo(App):
    def __init__(self): 
        super().__init__()
        Label("Help display", font="Arial 24")

        str = tk.__doc__
        App.text = Text(str, width=70)
             
Demo().run()

text8.py