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()

Output

When you run this code, you'll see a blank Text widget as below 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")

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

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

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

root.mainloop()

Output

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")

my_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 my_list:
   txt_output.insert(END, item + "\n")

root.mainloop()

Output

Take a look at the output window now −

Use 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 at the bottom of the code 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")

my_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 my_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()

Output

On execution, you will get the following output window −

Use 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")

my_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 my_list:
   txt_output.insert(END, item + "\n")

root.mainloop()

Output

On execution, you will get to see the following output window −

Conclusion

In conclusion, printing a list to a Tkinter Text widget is a simple task that can be useful for displaying data in a user-friendly format. By using the insert() method and a for loop, you can print each item in the list on a separate line. With a few extra formatting techniques, such as adding a title, using different text styles, or using indentation, you can make the output even more visually appealing and easier to understand.

Updated on: 13-Apr-2023

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements