The Wayback Machine - http://web.archive.org/web/20210203044125/https://www.geeksforgeeks.org/python-tkinter-text-widget/amp/

Python Tkinter – Text Widget

Tkinter is a GUI toolkit used in python to make user-friendly GUIs.Tkinter is the most commonly used and the most basic GUI framework available in python. Tkinter uses an object-oriented approach to make GUIs.

Note: For more information, refer to Python GUI – tkinter

Text Widget

Text Widget is used where a user wants to insert multiline text fields. This widget can be used for a variety of applications where the multiline text is required such as messaging, sending information or displaying information and many other tasks. We can insert media files such as images and links also in the Textwidget.

Syntax:

T = Text(root, bg, fg, bd, height, width, font, ..)

Optional parameters



Some Common methods

Tag handling methods

Mark handling methods

Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

import tkinter as tk
  
  
root = Tk()
  
# specify size of window.
root.geometry("250x170")
  
# Create text widget and specify size.
T = Text(root, height = 5, width = 52)
  
# Create label
l = Label(root, text = "Fact of the Day")
l.config(font =("Courier", 14))
  
Fact = """A man can be arrested in
Italy for wearing a skirt in public."""
  
# Create button for next text.
b1 = Button(root, text = "Next", )
  
# Create an Exit button.
b2 = Button(root, text = "Exit",
            command = root.destroy) 
  
l.pack()
T.pack()
b1.pack()
b2.pack()
  
# Insert The Fact.
T.insert(tk.END, Fact)
  
tk.mainloop()
chevron_right

Output

Example 2: Saving Text and performing operations

filter_none

edit
close

play_arrow

link
brightness_4
code

from tkinter import *
  
root = Tk()
root.geometry("300x300")
root.title(" Q&A ")
  
def Take_input():
    INPUT = inputtxt.get("1.0", "end-1c")
    print(INPUT)
    if(INPUT == "120"):
        Output.insert(END, 'Correct')
    else:
        Output.insert(END, "Wrong answer")
      
l = Label(text = "What is 24 * 5 ? ")
inputtxt = Text(root, height = 10,
                width = 25,
                bg = "light yellow")
  
Output = Text(root, height = 5, 
              width = 25, 
              bg = "light cyan")
  
Display = Button(root, height = 2,
                 width = 20, 
                 text ="Show",
                 command = lambda:Take_input())
  
l.pack()
inputtxt.pack()
Display.pack()
Output.pack()
  
mainloop()
chevron_right

Output

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




Article Tags :