tk_fonts_03.py

# =========================================================
# text with scrollbar
# =========================================================

import sys

# ---------------------------------------------------------
# global variables
# ---------------------------------------------------------

defaultFont = ('arial', 12)

# --- import Tkinter, ... based on which pervion of
# --- Python we are running

if sys.version_info.major is 3:
    from tkinter import *
    from tinker.font import *
    py3 = True
else:
    from Tkinter import *
    from tkFont import *
    py3 = False

# ---------------------------------------------------------
# main
# ---------------------------------------------------------

if __name__ == '__main__':

    # --- Tk root

    root = Tk()
    root.title('Display Fonts')

    # --- create a frame to hold everything

    frm = Frame(root, relief='flat', padx=4, pady=4, bg='red')
    frm.grid(row=0, column=0, sticky=N+S+E+W)

    Grid.columnconfigure(root, 0, weight=1)
    Grid.rowconfigure(root, 0, weight=1)

    # --- add a text area (widget) and scrollbar to the frame

    txt = Text(frm, relief='groove', borderwidth=2, bg='yellow',
            font=defaultFont, width=80, height=40)

    txt.pack(side=LEFT, fill='both', expand=True)

    scr = Scrollbar(frm)

    scr.config(command=txt.yview)

    txt.config(yscrollcommand=scr.set)

    scr.config(command=txt.yview)

    scr.pack(side=RIGHT, fill=Y)

    # -- insert test strings

    txt.insert(END,'a b c d ')
    ##txt.see(END)
    txt.insert(END,'1 2 3 4 5 6 7 8 9 0 ')
    txt.insert(END,'Now is the winter of our discontent made ' +
            'glorious summer by this son of York. ' +
            'Baa baa black sheep, have you any wool?\n\n')

    q1 = \
'''HAMLET: To be or no to be--that is the question:
Whether 'tis nobler in the mind to suffer
The slings and arrows of outrageous fortune
Or take arms against a sea of troubles
And by opposing end them. To die, to sleep--
No more--and by a sleep to say we end
The heartache, and the thousand natural shocks
That flesh is heir to. 'Tis a consummation
Devoutly to be wished.'''

    txt.insert(END,q1)

    # --- make the text area (widget) read-only
    # --- only the script can make changes

    ##txt.bind('<Key>', lambda e: 'break')


    ##i = 0
    ##for f in sorted(Tkf.families()):
    ##    i += 1
    ##    print('Font family: {}'.format(f))

    # -- event loop

    root.mainloop()