text tkinter code example

Example 1: tkinter textbox

import tkinter

textBox = tkinter.Enter(text="Placeholder text")

Example 2: how to get tkinder to display text

import tkinter as tk

root = tk.Tk()
tk.Label(root, 
		 text="Red Text in Times Font",
		 fg = "red",
		 font = "Times").pack()
tk.Label(root, 
		 text="Green Text in Helvetica Font",
		 fg = "light green",
		 bg = "dark green",
		 font = "Helvetica 16 bold italic").pack()
tk.Label(root, 
		 text="Blue Text in Verdana bold",
		 fg = "blue",
		 bg = "yellow",
		 font = "Verdana 10 bold").pack()

root.mainloop()

Example 3: text widget get tkinter

contents = text.get(1.0, END)

Example 4: tkinter set text

def set_input(value):
    text.delete(1.0, "END")
    text.insert("END", value)

Example 5: python tkinter text

# Code snippets for the Text widget
# ...to select and color text
# ...to re and undo changes
# ...to get cursor position and add code at the position
# ...to use the Font option in a more advanced way
# ...to add a x and y scrollbar
# ...to set a selection color

from tkinter import *
from tkinter.font import Font

def undotext(*event):
    text.edit_undo()

def redotext(*event):
    text.edit_redo()
    
def keycheck(*event):
    if event[0].char == '(':
        position = text.index(INSERT)
        text.insert(position, ')')
        text.icursor(position)
        
def coloredtext(*event):
    if event[0].keysym != 'Shift_L':
        return
    color_font = Font(text_calc, text_calc.cget("font"))
    text_calc.tag_configure("colored", font=color_font, foreground='#FF00FF')
    current_tags = text_calc.tag_names("sel.first")
    if "colored" in current_tags:
        text_calc.tag_remove("colored", "sel.first", "sel.last")
    else:
        text_calc.tag_add("colored", "sel.first", "sel.last")


calcfont = Font(family='Courier', size=14, weight='normal', slant='roman', underline=0, overstrike=0)

frame = Frame(master=tkFenster, bg='#FFFFFF')
frame.place(x=0, y=0, width=100, height=100)

scroll_y = Scrollbar(master=frame_calculator)
scroll_y.place(x=0, y=0, width=15, height=100)

scroll_calc_x = Scrollbar(master=frame_calculator, orient='horizontal')
scroll_calc_x.place(x=0, y=100, 100, height=15)

text = Text(master=frame, bg='#FFFFFF', fg='#000000', font=calcfont, borderwidth=0, undo=True, wrap='none',
            insertbackground='#00FF00', selectbackground='#0000FF', selectforeground='#000000',
            yscrollcommand=scrollc_y.set, xscrollcommand=scroll_x.set)
text.place(x=0, y=0, width=85, height=85)
text.bind('<Control-z>', undotext)
text.bind('<Control-y>', redotext)
text.bind('<KeyRelease>', keycheck)
text.bind('<KeyRelease>', coloredtext)
scroll_y.config(command=text.yview)
scroll_x.config(command=text.xview)