Tkinter, being the large and expansive GUI library that it is, offers us a wide range of widgets to take input in Python. One of these many widgets is the Tkinter Text Widget, which can be used to take multiline input. It also has a ton of amazing features that allow us to create complex GUI applications such as Notepad’s and Syntax Highlighters.
Another very popular alternative is the Tkinter Entry widget, used to take single line input. For most cases, it will be your preferred choice of input.
Creating the Text Widget
We’ve added in some extra parameters such as Height and Width for a custom size. We have also passed in expand = True and fill = tk.BOTH parameters into the frame and text widget, so that if we resize the window the size of the Text widget will expand in “both” the X and Y direction to adjust accordingly.
import tkinter as tk
class Window:
def __init__(self, master):
self.master = master
self.frame = tk.Frame(self.master)
self.frame.pack(expand = True, fill = tk.BOTH)
self.label = tk.Label(self.frame, text = "My NotePad")
self.label.pack()
self.text = tk.Text(self.frame, undo = True, height = 20, width = 70)
self.text.pack(expand = True, fill = tk.BOTH)
root = tk.Tk()
window = Window(root)
root.mainloop()
Below is the output of the above code:
