Printing a list to a Tkinter Text widget

When creating a graphical user interface (GUI) in Python with Tkinter, it's common to want to display data in a user-friendly format. One way to do this is to print a list of items to a Tkinter Text widget, which allows you to display the list with each item on a separate line. In this article, we'll cover how to print a list to a Tkinter Text widget and some tips for formatting the output.

Creating a Tkinter Text Widget

Before we can print a list to a Tkinter Text widget, we need to create the widget itself. Here's some example code to create a simple Text widget −

from tkinter import *

root = Tk()
root.geometry("720x250")
root.title("Creating a Tkinter Text Widget")

txt_output = Text(root, height=5, width=30)
txt_output.pack(pady=30)

root.mainloop()

When you run this code, you'll see a blank Text widget with enough space to display 5 lines of text, each with a maximum width of 30 characters.

Printing a List to the Tkinter Text Widget

Once we have a Text widget, we can print a list of items to it. The easiest way to do this is to iterate over the list using a for loop, and use the insert() method of the Text widget to add each item to a new line. Below is the example code that prints a list of fruit names to the Text widget −

from tkinter import *

root = Tk()
root.geometry("720x250")
root.title("Printing a List to the Tkinter Text Widget")

fruit_list = ["apple", "banana", "pineapple", "mango"]

txt_output = Text(root, height=5, width=30)
txt_output.pack(pady=30)

for item in fruit_list:
    txt_output.insert(END, item + "\n")

root.mainloop()

When you run this code, the Text widget will display the four fruit names, each on a separate line.

Formatting the Output

Printing a list to a Tkinter Text widget is a simple task, but there are several ways you can format the output to make it more visually appealing.

Adding a Title or Heading to the List

If the list of items you're printing has a specific meaning or purpose, you might want to add a title or heading to the Text widget to make it clear to the user. You can do this by using the insert() method to add a line of text to the top of the widget, before the list itself. Here's an example −

from tkinter import *

root = Tk()
root.geometry("720x250")
root.title("Adding a Title or Heading to the List")

fruit_list = ["apple", "banana", "pineapple", "mango"]

txt_output = Text(root, height=6, width=30)
txt_output.pack(pady=30)

txt_output.insert(END, "List of Fruits\n")
for item in fruit_list:
    txt_output.insert(END, item + "\n")

root.mainloop()

Using Formatting to Distinguish Between Items

Sometimes it's useful to format the output in a way that makes it easier to distinguish between items in the list. One way to do this is to use different text styles, such as bold or italic, to highlight certain words or phrases.

In this example, we add a bullet point to the word "banana" using the "*" character, and we format it using a custom bold tag. We define the tag using the tag_configure() method, which specifies the font style and weight −

from tkinter import *

root = Tk()
root.geometry("720x250")
root.title("Use Formatting to Distinguish Between Items")

fruit_list = ["apple", "banana", "pineapple", "mango"]

txt_output = Text(root, height=6, width=30)
txt_output.pack(pady=30)

txt_output.insert(END, "List of Fruits\n\n")
for item in fruit_list:
    if item == "banana":
        txt_output.insert(END, "* " + item + "\n", "bold")
    else:
        txt_output.insert(END, "- " + item + "\n")

txt_output.tag_configure("bold", font=("Arial", 10, "bold"))
root.mainloop()

Using Indentation to Show Hierarchy

If your list has a hierarchical structure, you might want to use indentation to make it clear which items are subordinate to others. You can do this by adding spaces or tabs before each item that should be indented.

In this example, we create a list that contains two main categories (fruits and vegetables), with three items under each category. We add two spaces before each item that should be indented, which makes it clear which items are part of each category −

from tkinter import *

root = Tk()
root.geometry("720x200")
root.title("Use Indentation to Show Hierarchy")

category_list = ["fruits",
   "  - apple", "  - banana", "  - pineapple",
   "vegetables",
   "  - cabbage", "  - tomato", "  - onion"]

txt_output = Text(root, height=8, width=30)
txt_output.pack(pady=30)

for item in category_list:
    txt_output.insert(END, item + "\n")

root.mainloop()

Alternative Method: Using join()

Another approach is to use Python's join() method to convert the entire list to a single string, then insert it all at once −

from tkinter import *

root = Tk()
root.geometry("720x250")
root.title("Using join() Method")

fruit_list = ["apple", "banana", "pineapple", "mango"]

txt_output = Text(root, height=6, width=30)
txt_output.pack(pady=30)

# Convert list to string with newlines
list_text = "\n".join(fruit_list)
txt_output.insert(END, "Fruits:\n" + list_text)

root.mainloop()

Conclusion

Printing a list to a Tkinter Text widget is accomplished using the insert() method with a for loop or the join() method. You can enhance the display with formatting techniques like titles, text styles, and indentation to create more visually appealing and organized output.

Updated on: 2026-03-26T18:49:45+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements