NoneType 错误 self.widget.insert [英] NoneType Error self.widget.insert
本文介绍了NoneType 错误 self.widget.insert的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在我的应用程序中有一个输出框架.当我运行它时,我收到错误:NoneType object has no attribute insert
on self.widget.insert('end', string)
.任何帮助,将不胜感激.
I am trying to have an output frame in my application. When I run it i get the error: NoneType object has no attribute insert
on self.widget.insert('end', string)
. Any help would be appreciated.
import Tkinter as tk
import sys
class Test(tk.Tk):
def __init__(self):
tk.Tk.__init__(self)
toolbar = tk.Frame(self).grid()
tk.Button(self, text="print to stdout", command=self.print_stdout).grid()
self.text= tk.Text(self).grid()
sys.stdout= Output(self.text)
def print_stdout(self):
print "Hello"
print "This is test"
class Output(object):
def __init__(self, widget):
self.widget = widget
def write(self,string):
self.widget.insert('end', string)
app = Test()
app.mainloop()
推荐答案
你的问题出现在这一行:
Your problem occurs on this line:
self.text= tk.Text(self).grid()
grid
没有明确地return
任何东西,所以这有效地设置了self.text = None
.然后将该值传递给 Output.__init__
并最终在 write
中访问.
grid
doesn't explicitly return
anything, so this is effectively setting self.text = None
. This value is then passed to Output.__init__
and eventually accessed in write
.
改为分为两步:
self.text = tk.Text(self)
self.text.grid()
这篇关于NoneType 错误 self.widget.insert的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文