0

The following code works for Entry(), but when I use ScrolledText() it fails to work.

from tkinter import *
from tkinter.scrolledtext import *

def click():
    a = text1.get()
    print(a)

window = Tk()
window.title("TITLE PLACEHOLDER")
Label(window,text="Subject", bg="black",fg="white", font="Arial 10").grid(row=1,column=0,sticky=W)
text1 = ScrolledText(window,bg="white", width=50,font="Arial 10")
text1.grid(row=2,column=0,sticky=W)
Button(window, text="send",width=6,command=click).grid(row=3,column=0,sticky=W)
window.mainloop()

Error:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\user\AppData\Local\Programs\Python\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
    return self.func(*args)
  File "C:\Users\user\Desktop\folder_name\testing.py", line 5, in click
    a = text1.get()
TypeError: get() missing 1 required positional argument: 'index1'

Any suggestions?

Miles Kent
  • 48
  • 8

1 Answers1

3

As your error says get() is missing required positional arguments, you need to specify the start and end of text selection from the text entered.

The parameters you need to pass to the get() method:

text1.get('1.0', 'end-1c')

Here 1.0 means start getting text from first character of the first line and end-1c means select text till the end and -1c means removing 1 character from the end as a newline \n character is added at the end of the text. In simple words it will select all the text you entered.

The working code:

from tkinter import *
from tkinter.scrolledtext import *

def click():
    a = text1.get('1.0', 'end-1c')
    print(a)

window = Tk()
window.title("TITLE PLACEHOLDER")
Label(window,text="Subject", bg="black",fg="white", font="Arial 10").grid(row=1,column=0,sticky=W)
text1 = ScrolledText(window,bg="white", width=50,font="Arial 10")
text1.grid(row=2,column=0,sticky=W)
Button(window, text="send",width=6,command=click).grid(row=3,column=0,sticky=W)
window.mainloop()

Screenshot

Hope it helps!

Somraj Chowdhury
  • 983
  • 1
  • 6
  • 14