Python Tkinter - ScrolledText

Pybeginner
By -
0


We can also create a Scrolled Text area in tkinter. To add a ScrolledText widget, you can use the ScrolledText class like this:

 

from tkinter import scrolledtext

txt = scrolledtext.ScrolledText(window, width = 40, height = 10)

 

Here, we specify the width and height of the ScrolledText widget otherwise, it will fill the entire window.

So the Complete code will look like this:

 

from tkinter import *

from tkinter import scrolledtext

window = Tk ()

window.title ("scrolledtext")

window.geometry ('350x200')

txt = scrolledtext.ScrolledText(window, width = 40, height = 10)

txt.grid( column = 0, row = 0, padx=10, pady=10)
window.mainloop()

 




Python Tkinter - Define content of scrolled text

 

To define the content of scrolled text, you can use the insertion method like this:

txt.insert(INSERT, 'Your text goes here' )

 

See the following Example:

 

from tkinter import *

from tkinter import scrolledtext

window = Tk()

window.title("scrolledtext")

window.geometry('350x200')

txt = scrolledtext.ScrolledText(window, width=40, height=10)
txt.grid(column=0, row=0, padx=10, pady=10)


txt.insert(INSERT, 'if i were freedom . I gave a little of myself to all living beings To do good. To children, to play. To young people, to have fun. To politicians, to understand each other. To plants, to grow. To little birds, to fly in the high skies. If I were freedom, I would like men to unite.')


window.mainloop()

 



Python Tkinter - Delete or  Clear scrolled text content

 

To clear the content of a scrolled text widget, you can use the delete method like this:

txt.delete (1.0, END)

Post a Comment

0Comments

Post a Comment (0)