Python 3 – Tkinter Text
When it comes to creating graphical interfaces for Python programs, the Tkinter library is a popular choice. With Tkinter, you can create windows, dialogs, buttons, menus, and much more. One of the Tkinter widgets that you can use is the Text widget. In this article, we’ll take a closer look at the Tkinter Text widget and how you can use it in your own Python programs.
Overview of the Tkinter Text widget
The Tkinter Text widget is used to display and edit multiline text. It provides a scrollable area where users can type and manipulate text. The Text widget supports various formatting options, such as bold, italic, underline, and font size. You can also insert images and checkboxes into the text area.
Here is an example code snippet that creates a Text widget and sets its font size and background color:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root, font=("Helvetica", 12), bg="white")
text.pack(fill="both", expand=True)
root.mainloop()
In this example, we created a new Text
widget and assigned it to the text
variable. We set its font size to 12pt using the font
parameter and its background color to white using the bg
parameter. The pack
method is used to fill the available space in the parent widget (in this case, the root window).
Adding text to the Text widget
To add text to a Text widget, you can use the insert
method. The insert
method takes two arguments: the position where the text should be inserted (specified as a string, such as "1.0"
, meaning the first line and the first character) and the text to be inserted.
Here is an example code snippet that inserts some text into a Text widget:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root, font=("Helvetica", 12), bg="white")
text.pack(fill="both", expand=True)
text.insert("insert", "Hello, world!")
root.mainloop()
In this example, we used the "insert"
position, which will insert the text at the current cursor location. We passed the text "Hello, world!"
as the second argument to the insert
method.
Reading text from the Text widget
You can read the text in a Text widget using the get
method. The get
method takes two arguments: the start position and the end position of the text to be retrieved. If you omit the end position, the entire text in the widget will be returned.
Here is an example code snippet that retrieves the text from a Text widget and displays it in a label widget:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root, font=("Helvetica", 12), bg="white")
text.pack(fill="both", expand=True)
text.insert("insert", "Hello, world!")
label = tk.Label(root, text="")
label.pack()
def show_text():
text_value = text.get("1.0", "end")
label.config(text=text_value)
button = tk.Button(root, text="Show text", command=show_text)
button.pack()
root.mainloop()
In this example, we added a button and a label widget to the window. We defined a function called show_text
that retrieves the text from the Text widget using the get
method and sets the text of the label widget using the config
method. We then linked the show_text
function to the button using the command
parameter.
Formatting text in the Text widget
You can format text in a Text widget using the tag_add
and tag_config
methods. A tag is a name that you give to a range of characters in the Text widget. You can then apply formatting options to these tags, such as font, size, color, or background color.
Here is an example code snippet that formats some text in a Text widget:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root, font=("Helvetica", 12), bg="white")
text.pack(fill="both", expand=True)
text.insert("insert", "Hello, world!")
text.tag_add("bold", "1.0", "1.5")
text.tag_config("bold", font=("Helvetica", 12, "bold"))
root.mainloop()
In this example, we added a tag called "bold"
to the first five characters of the Text widget. We then configured the "bold"
tag to be bold using the tag_config
method.
Adding images to the Text widget
You can add images to a Text widget using the image_create
method. The image_create
method takes two arguments: the position where the image should be inserted and the image object.
Here is an example code snippet that adds an image to a Text widget:
import tkinter as tk
root = tk.Tk()
text = tk.Text(root, font=("Helvetica", 12), bg="white")
text.pack(fill="both", expand=True)
text.insert("insert", "Hello, world!")
photo = tk.PhotoImage(file="image.gif")
text.image_create("end", image=photo)
root.mainloop()
In this example, we added an image to the end of the Text widget. We created a PhotoImage
object from a file called "image.gif"
, and then passed the PhotoImage
object as the second argument to the image_create
method.
Conclusion
In summary, the Tkinter Text widget provides a powerful tool for displaying and editing text in Python programs. With its extensive formatting options, you can create rich text layouts with ease. By using the various methods provided by the Text widget, you can insert text, retrieve text, format text, add images, and much more. We hope this article has given you a good introduction to the capabilities of the Tkinter Text widget and how you can use it to enhance the user interface of your Python programs.