我是一个新的程序员,如果你能帮助我,我将不胜感激。我看过谷歌,但有一些“Twisted+Tkinter”的例子。当我单击窗口上的“发送”按钮时,出现此错误:
错误是 e.插入(0,米) AttributeError: 'str' 对象没有属性 'insert'
还有代码:
from Tkinter import *
from twisted.internet import reactor, tksupport
class App(object):
def onQuit(self):
print "Quit!"
reactor.stop()
def onButton(self):
m=self.entryvar.get()
e=self.labeltext.get()
e.insert(0,m)
def __init__(self, master):
frame = Frame(master)
frame.pack()
self.entryvar=StringVar()
self.mes=Entry(frame,textvariable=self.entryvar)
self.mes.pack()
self.labeltext=StringVar()
self.label=Label(frame,textvariable=self.labeltext)
self.label.pack()
q = Button(frame, text="Quit!", command=self.onQuit)
b = Button(frame, text="Send", command=self.onButton)
q.pack(side=LEFT)
b.pack(side=LEFT)
if __name__ == '__main__':
root = Tk()
tksupport.install(root)
app = App(root)
reactor.run()
最佳答案
如果你想更新labeltext
StringVar
,你需要为它构造一个新字符串值。 str
是一个不可变序列类型,与 list
不同,它不能就地修改。
在这种情况下,也许您打算使用字符串连接?下面的代码会将labeltext
设置为m + e
,其中e
是labeltext
之前的内容,有效的前置m
:
def onButton(self):
m=self.entryvar.get()
e=self.labeltext.get()
self.labeltext.set(m + e)
关于python - 属性错误 : 'str' object has no attribute 'insert' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15135794/