Pascal.S
Pascal.S

Reputation: 81

Python Tkinter ScrolledText

I'm trying to make a Tkinter entry box, but need more space than just one line. It seems that

self.scroll = ScrolledText.ScrolledText(self.tk).pack()

is the best looking way to do it right now, but I dont know how to get the inputted text from self.scroll and use it for something else, theres no clear documentation on it either. Does anyone know?

Upvotes: 5

Views: 31178

Answers (3)

typedecker
typedecker

Reputation: 1390

To get the inputted text from within a Scrolltext widget in tkinter, the same get method is used as that of the Text widget(similar question). The two parameters of the function are the START and the END of the text extraction. In your use case, it seems you want to fetch the entire text which can be done by extracting text from line index "1.0" to tkinter.END, which is a constant that always points to the last line index.(for more info on line indices see this.)

get method syntax -:

widget.get("1.0", tkinter.END)

In your case it will become -:

self.scroll.get("1.0", tkinter.END)

Further, as pointed out by @furas, the pack method should be called separately as pack returns None, for more info refer to this thread. Fixed code for that section will be -:

self.scroll = ScrolledText.ScrolledText(self.tk)
self.scroll.pack()

Upvotes: 0

furas
furas

Reputation: 143135

Mistake:

self.scroll = ScrolledText.ScrolledText(self.tk).pack()

this way you assign pack() result to self.scroll (not ScrolledText)
and pack() always returns None.

Always:

self.scroll = ScrolledText.ScrolledText(self.tk)
self.scroll.pack()

And now see standard Text Widget documentation how to get/set text.

from tkinter import *
import tkinter.scrolledtext as ScrolledText

master = Tk()

st = ScrolledText.ScrolledText(master)
st.pack()

st.insert(INSERT, "Some text")
st.insert(END, " in ScrolledText")

print(st.get(1.0, END))

master.mainloop()

Upvotes: 14

incalite
incalite

Reputation: 3207

You can have mutliple lines by tweaking the height parameter:

sText = ScrolledText.ScrolledText(root, height=15)
sText.pack()

Get the content using:

words = sText.get(1.0,END)

Hope that helps!

Upvotes: 2

Related Questions