Printing a list to a Tkinter Text widget

TkinterPythonGUI-Programming

The Tkinter Text widget is used to take multiline user text input. It provides many properties and built-in functions that can be used to customize the text widget. Let's suppose we need to create an application in which we want to display a list of items in a Text widget. To insert a list of items in a Text widget, we have to iterate over each item in the list and insert them in the Text widget. The following example demonstrates how to implement it.

Example

# Import the required library
from tkinter import *

# Create an instance of tkinter frame
win=Tk()

# Set the geometry
win.geometry("700x350")

# Add the list of items
days= ["Sun", "Mon", "Tue","Wed","Thu","Fri","Sat"]

text=Text(win, width=80, height=15)
text.pack()

# Iterate over each item in the list
for day in days:
   text.insert(END, day + '\n')
   
win.mainloop()

Output

Running the above code will display the days of week as the list in a text widget.

raja
Updated on 16-Dec-2021 11:07:08

Advertisements