用Tkinter插入文本框 - python

我正在尝试使用Tkinter将文本插入文本框。我正在尝试插入从文件中检索到的信息,但将其归结为一个更简单的方法,它表现出相同的问题:

class Main(Frame):
    def __init__(self, parent):
        Frame.__init__(self, parent)
        self.parent = parent
        self.init_ui()

    def helloCallBack(self):
        self.txt.insert("lol")

    def init_ui(self):    
        self.txt = Text(root, width=24, height = 10).grid(column=0, row = 0)

        load_button = Button(root, text="Load Wave Data", command = self.helloCallBack).grid(column=1, row = 0, sticky="E")

def main():
    ex = Main(root)
    root.geometry("300x250+300+300")
    root.mainloop()

我想要做的是每当我按下按钮时,它将lol插入文本框中,但出现错误

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

我该如何解决?

python大神给出的解决方案

您需要在单独的行中调用grid。因为该方法返回None;导致self.txt引用None而不是Text小部件对象。

def init_ui(self):
    self.txt = Text(root, width=24, height=10)
    self.txt.grid(column=0, row=0)

    load_button = Button(root, text="Load Wave Data", command=self.helloCallBack)
    load_button.grid(column=1, row=0, sticky="E")

您需要指定插入文本的位置。

def helloCallBack(self):
    self.txt.insert(END, "lol")