I have looked at most of the available Tkinter ScrolledText StackOverflow posts including descriptions of inheritance found : Inheritance Tutorial and Inheritance with Classes. Yet, I cannot seem to understand in this specific example, why I get the following error:
textBoxClass(self.parent).textDropIn(self.parent).insert(tk.INSERT,"This is the text to add.")
AttributeError: 'NoneType' object has no attribute 'insert'
I understand that I do not have the attribute of 'insert'
present, but I do not understand why the textDropIn
function does not have the attributes from ScrolledText
based on my class definition call of class textBoxClass(tkst.ScrolledText):
, however I suspect it is improper instantiation (?) that is the reason why the inheritance of the ScrolledText
attributes are not available in the function.
Another part of me suspects that I have to inherit attributes from ScrolledText
within the someGui
class in order to call them in the class methods, but I am not sure.
Full code:
from tkinter import *
from tkinter import ttk
import tkinter as tk
import tkinter.scrolledtext as tkst
class someGui(tk.Tk):
def __init__(self,parent):
self.parent=parent
self.Window()
textBoxInstance=textBoxClass(self.parent)
def Window(self):
self.parent.configure(bg='white')
self.parent.geometry("1000x500")
self.parent.title("Example Window")
self.someFrame = ttk.Frame(self.parent)
self.someFrame.grid(row=0, column=0, sticky=(N,S,E,W))
textBoxSeparate=textBoxClass(self.parent)
self.someFunction()
def someFunction(self):
#otherstuff
textBoxClass(self.parent).textDropIn(self.parent).insert(tk.INSERT,"This is the text to add.")
class textBoxClass(tkst.ScrolledText):
def __init__(self,parent):
self.root=parent
self.textDropIn(self.root)
def textDropIn(self,parent):
self.someText = tkst.ScrolledText(master=self.root, wrap=tk.WORD, width=50, height=20)
self.someText.grid(row=0, column=4, rowspan=7, columnspan=4, pady=20, padx=20)
def main():
root =tk.Tk()
sg=someGui(root)
root.mainloop()
if __name__=='__main__':
main()
This question has been marked as a duplicate as another tkinter python post with a None
return in the context of a get()
attribute call, but I have made the same line separation edit that was recommended to that user without fixing the problem. If someone can explain, in detail, why it is a duplicate, I would be happy to remove the question. But, I cannot understand why it is.
Edit based on first answer by Bryan
This was my understanding. I made edits but ran into several errors along the way. I removed the tkst.ScrolledText
because I was improperly inheriting attributes and calling and instance of it. I removed parent
as an attribute in textDropIn
function and its respective call in __init__
in the textBoxClass
definition. I have also added the self.textBox=textBoxClass(self.parent)
to the __init__
of the someGui
class, but I have run into TypeError
and RecursionError
based on my edits. At present, it is a RecursionError
with the code in the current version. Which is on account of the self.textBox.textDropIn()
with no arguments passed through.
from tkinter import ttk
import tkinter as tk
import tkinter.scrolledtext as tkst
class someGui(tk.Tk):
def __init__(self,parent):
self.parent=parent
self.Window()
self.textBox=textBoxClass(self.parent) #saving the instance
def Window(self):
self.parent.configure(bg='white')
self.parent.geometry("1000x500")
self.parent.title("Example Window")
self.someFrame = ttk.Frame(self.parent)
self.someFrame.grid(row=0, column=0, sticky='nesw') #changed sticky definition for tk requirements
textBoxSeparate=textBoxClass(self.parent) # the initial inclusion of the textbox in the frame
self.someFunction() #no input needed
def someFunction(self):
#otherstuff
self.textBox.textDropIn() #there is no parent attribute in textDropIn, so I removed it
self.textBox.insert(tk.INSERT, "Some test text.") #split call to two lines and changed to tk.INSERT
class textBoxClass(): #removed tkst.ScrolledText in class call because instance was created in textDropIn
def __init__(self,parent):
self.root=parent
super().__init__() #kept receiving TypeError: object.__init__() takes no arguments, thus removed args
self.textDropIn() #removed parent attribute from function call
def textDropIn(self): #removed parent attribute from definition
self.someText = tkst.ScrolledText(master=self.root, wrap=tk.WORD, width=50, height=20)
self.someText.grid(row=0, column=4, rowspan=7, columnspan=4, pady=20, padx=20)
def main():
root =tk.Tk()
sg=someGui(root)
root.mainloop()
if __name__=='__main__':
main()