5

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?

Pascal.S
  • 81
  • 1
  • 1
  • 2
  • Does this answer your question? [How to create a multiline entry with tkinter?](https://stackoverflow.com/questions/9661854/how-to-create-a-multiline-entry-with-tkinter) – typedecker Feb 21 '22 at 13:29
  • The question and answers can use syntax that is more readable as follows: `from tkinter.scrolledtext import ScrolledText` Then you can use the `ScrolledText` class readily (instead of typing `ScrolledText.ScrolledText`). – Poikilos Aug 14 '23 at 14:30

3 Answers3

13

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()
wovano
  • 4,543
  • 5
  • 22
  • 49
furas
  • 134,197
  • 12
  • 106
  • 148
2

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!

incalite
  • 3,077
  • 3
  • 26
  • 32
0

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()
typedecker
  • 1,351
  • 2
  • 13
  • 25