-1

I can't seem to print the contents of the ScrolledText Widget to the console.

I have run the help command on the ScrolledText Widget:

print(help(scrolledtext.ScrolledText))

This outputs that the .get method (used to get the contents of tk.Text Widgets) is present in this widget aswell.

 |  get(self, index1, index2=None)
 |      Return the text from INDEX1 to INDEX2 (not included).

So why is it then that the line:

print(myScrollTextWidget.get("1.0","end"))

Results in this error:

AttributeError: 'NoneType' object has no attribute 'get'

When the button is pressed.

Here is a runnable code snippet that I'm working from:

import tkinter as tk
from tkinter import scrolledtext
#Declare Root
root = tk.Tk()
root.title("Scrolltext Widget")
tk.Label(root,text="My Scrolled Text Widget",font=("Times New Roman",25))\
    .grid(row=0,column=1)
#Define ScrollTextWidget
#wrap keyword used to wrap around text
myScrollTextWidget = scrolledtext.ScrolledText(root,wrap=tk.WORD,width=50,height=20,font=("Times New Roman",15))\
    .grid(row=1,column=1)
print(help(scrolledtext.ScrolledText))
def printToConsole():
    print(myScrollTextWidget.get("1.0","end"))
#Buttons
myButton = tk.Button(root,text="Print to console!",command=printToConsole).grid(row=2,column=1)


root.mainloop()
Lyra Orwell
  • 1,048
  • 4
  • 17
  • 46

1 Answers1

0

When you .pack() or .grid() in the sameline, you do not return anything to the variable thus the variable is None

Instead, .pack() or .grid() in another line as shown:

myScrollTextWidget = scrolledtext.ScrolledText(root,wrap=tk.WORD,width=50,height=20,font=("Times New Roman",15))
myScrollTextWidget.grid(row=1,column=1)

Generally, gridding or packing in the same line is quite erroneous. You should avoid it if you want to access your objects for configuration or other purposes.

ahmetknk
  • 308
  • 1
  • 7