Updated March 10, 2023
Introduction to Tkinter Scrolledtext
The Tkinter.scrolledtext is a better way to position things on the screen, which one will use most of the time. ScrolledText widget is like a vertical scroll bar on its right, a text budget. It can even be added to the horizontal bar. These bars disappear automatically when not in need. Also, it can be moved to the other side of the window, and other actions can be performed.
Text widget passes configuration options. The widget Frame is inserted between the text and the master, which holds the widget Scrollbar. Text widgets inherit most of the methods calls. However, Widgets like Place, Grid, and Pack redirected to the Frame widget.
Syntax of Tkinter:
To create this widget, all you need is a simple syntax:
w = text (master, any opting changes)
Parameters describing the Tkinter Widget:
Master: Represents Parent window
Options: The list below describes the commonly used widgets options. These are key-value paired, separated by commas.
Example of Tkinter Scrolledtext
ScrolledText is an import of the Tkinter widget, which allows the user to interact with the textbox directly.
import tkinter as tk
import tkinter.scrolledtext as tkk
win = tk.Tk()
frame1 = tk.Frame(
master = win,
bg = '#808000'
)
frame1.pack(fill='both', expand='yes')
editArea = tkk.ScrolledText(
master = frame1,
wrap = tk.WORD,
width = 20,
height = 20
)
editArea.pack(padx=10, pady=10, fill=tk.BOTH, expand=True)
#Text is added so as to confirm the working of scroll
editArea.insert(tk.INSERT,
"""\
With Great Power Comes Great Responsibility!
""")
win.mainloop()
Output:
So now, in the text box, we will add some text, modify, and change the text’s format.
import tkinter as tk
root = tk.Tk()
root.title(“GUI Text”)
text1 = tk.Text(root, height = 10, width = 50)
text1.config(state = “disabled”)
text1.pack()
root.mainloop()
Output:
This is essentially a textbox that none can type in.
In line 7 from the code, what stops us from typing in is that we essentially set the state to “disabled.”
By default, the textbox is usually enabled or otherwise known as normal.
If that is run excluding line 7, the output of the textbox is allowed to interact with or type in.
So, when we disable the textbox, we are disabling anyone from accessing it.
Now it can also be enabled using a command called INSERT, one of the modules of Tkinter. This enables the user to insert text lines in the textbox we output.
In real-time, you use games to take in their names and then disable the textboxes so that not even the user can change the name again.
import tkinter as tk
root = tk.Tk()
root.title(“GUI Text”)
text1 = tk.Text(root, height = 10, width = 50)
text1.config(state = “normal”)
text1.insert(tk.INSERT,”Line 1\n”)
text1.insert(tk.INSERT,”Line 2”)
text1.config(state = “disabled”)
text1.pack()
root.mainloop()
Output:
Here, you insert the text into the textbox using INSERT. First, it is configured to the normal state. This is usually normal or defaulted to type in textbox. Then, after inserting text, you disable the textbox to be editable.
The first line says LINE 1
The second line says LINE 2
This is how it is in real-world understanding:
012345
1 Line 1
2 Line 2
Line 1 is in the First line, and the L starts from index 0 and ends with index 5.
So, if anyone wants to insert text into the in-between lines, it can be something like this:
text1.insert(1.1, “*”)
which says, insert an asterisk in line 1 and index 1. And the output is shown something like this:
Now, let’s create a canvas object. Canvas is a widget we will create using the tk interface.
The canvas widget is essentially a section where you can draw things on.
import tkinter as tk
root = tk.Tk() #which constructs a new window
root.title(“GUI Canvas”) #Title of the window is going to be this
root.resizable = (False, False)
canvas1 = tk.Canvas (root , width = 300, height = 500, background = “black”)
canvas1.pack()
root.mainloop()
The canvas display will be of width 300 and a height of 500, which is not resizable. By default, all windows are resizable. But here, as we set the resizable parameters to be false in width and height, they are not changeable.
The other way to make it easier is by setting parameter limits, which allows the window to be resizable only at that particular range.
Let’s create a slider in the GUI interface:
import tkinter as tk
root = tk.Tk()
root.tilte(“GUI Slider”)
slide1 = tk.Scale (root, from_=0, to = 10, orientation = tk.HORIZONTAL)
slide1.pack()
root.mainloop()
If I run this,
The slider is interactive. You can also change the resolution and orientation to the vertical range.
Conclusion
A GUI toolkit is used to import Tkinter. That which has a scrolled widget text is called a ScrolledText. ScrolledText is not something one cannot do himself. It is quite simple and uses a few lines of code. This allows the display of texts in large amounts in a smaller space.
Recommended Articles
This is a guide to Tkinter Scrolledtext. Here we discuss the Examples of Tkinter Scrolledtext along with the syntax and parameters. You may also have a look at the following articles to learn more –