Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content
No results
  • About AskPython
  • Blog
  • Contact Me
  • Free Online Developer Tools Hub
  • Home
  • Meet the Team
  • Privacy Policy
  • Python
  • Python Course for Beginners Online FREE
  • Python Tutorial – Mega Index
  • Terms and Conditions

🚀 Supercharge your YouTube channel's growth with AI.

Try YTGrowAI Free
  • Home
  • Blog
  • About AskPython
  • Python Course
  • Start Here

Tkinter Text Widget with Tkinter Scrollbar

  • Vijaykrishna RamVijaykrishna Ram
  • April 24, 2020
  • Tkinter
Tkinter Text Widget with Tkinter Scrollbar

Hello everyone. In this part of our Tkinter tutorial section, we’ll take a look at the Tkinter Text Widget.

While I had been using an object oriented approach in the previous tutorials, since we’ll be looking at individual widget examples now, it would be simpler if I directly used the module.

So, I’ll be directly using Tkinter here, without an explicit Application class.

Now, let’s get started!


Tkinter Text Widget

This is a widget that allows us to easily view text objects in our application. For example, if you wanted to display some lines on the GUI, we can easily use a Text widget to achieve our objective.

Let’s see how.

To create a text widget, simply use the tk.Text(master, width, height) method, where master is the master object of the GUI application (using tk.TK()).

We can then add text by using the text_widget.insert() method.

import tkinter as tk

# Create our master object to the Application
master = tk.Tk()

# Create the text widget for two lines of text
text_widget = tk.Text(master, height=2, width=40)

# Pack it into our tkinter application
text_widget.pack()

# Insert text into the text widget
# tk.END specifies insertion after the last character in our buffer
text_widget.insert(tk.END, "First Line - Hello from AskPython\nSecond Line - Hi")

# Start the mainloop
tk.mainloop()

Output

Tkinter Text Widget 1
Tkinter Text Widget 1

Indeed, we seem to have the two lines of text in our text widget!

Adding a scrollbar to our Text Widget

While the above output works for smaller texts, what if our text size is itself larger than the width?

We can use Tkinter’s scrollbar and add it to our text widget. Now, after adding the scrollbar, we should be able to display larger texts properly.

To create a scrollbar object, use tk.Scrollbar() and add it to our application! Now, after you pack it to the application, we can display longer texts using a scrolling text widget!

import tkinter as tk

# Create our master object to the Application
master = tk.Tk()

# Create the text widget
text_widget = tk.Text(master, height=5, width=40)

# Create a scrollbar
scroll_bar = tk.Scrollbar(master)

# Pack the scroll bar
# Place it to the right side, using tk.RIGHT
scroll_bar.pack(side=tk.RIGHT)

# Pack it into our tkinter application
# Place the text widget to the left side
text_widget.pack(side=tk.LEFT)

long_text = """This is a multiline string.
We can write this in multiple lines too!
Hello from AskPython. This is the third line.
This is the fourth line. Although the length of the text is longer than
the width, we can use tkinter's scrollbar to solve this problem!
"""

# Insert text into the text widget
text_widget.insert(tk.END, long_text)

# Start the mainloop
tk.mainloop()

Output

Tkinter Scroll 1
Tkinter Scroll 1
Tkinter Scroll 2
Tkinter Scroll 2

You can see the scrollbar to the right, supported by the text widget to the left.

Hopefully, you can build upon this to add more widgets to your GUI application, and make it more interesting!


Conclusion

In this tutorial, we learned how we could add simple text widgets to our Tkinter Application, and also add scrollbars to support larger texts.

Stay tuned for more widgets, in our upcoming Tkinter tutorials!


Vijaykrishna Ram
Vijaykrishna Ram
Articles: 99
Understanding the super() Method in Python
Previous Post Understanding the super() Method in Python
Next Post Python Directory Listing
Python Directory Listing

Related Posts

Tkinter IntVar – Tkinter Tutorial with Examples

Tkinter IntVar – Tkinter Tutorial with Examples

  • July 5, 2026
Tkinter StringVar with Examples – Tkinter Tutorial

Tkinter StringVar with Examples – Tkinter Tutorial

  • July 5, 2026
Tkinter GUI Widgets

Tkinter GUI Widgets

  • April 24, 2026

Partner Websites

  • GoLangDocs
  • LinuxForDevices
  • CodeForGeek

Popular Categories

  • Python Programming
  • Python Modules
  • OOPS in Python
  • Python Programming Examples
  • Strings in Python

Hall of Fame

  • Python Programming
  • OOPS in Python
  • Creating Python Modules
  • Args and Kwargs in Python
  • Python Dictionary (Dict) Tutorial
  • Python List – 15 Things You MUST Know
  • Python Set – Things You MUST Know
  • Python String Functions: Methods by Task

Important Links

  • Home
  • Python Course
  • Start Here
  • Blog
  • About AskPython
Latest posts
  • np.linalg.eig: Eigenvalues and Eigenvectors
  • What Does %s Mean in a Python Format String?
  • How to Use np.sin() in NumPy
  • cv2.imread: Load Images with OpenCV
  • pd.read_json: Read JSON Into a DataFrame

Copyright © 2026 - AskPython. All rights reserved.