python - 属性错误 : 'str' object has no attribute 'insert'

标签 python tkinter twisted

我是一个新的程序员,如果你能帮助我,我将不胜感激。我看过谷歌,但有一些“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,其中elabeltext之前的内容,有效的前置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/

相关文章:

python - 访问 .tar 文件中的 .tar 文件

python - 关闭顶级 Tkinter 窗口

python - TypeError : 'CurrencyConverter' object is not callable

python - 使用 "pointer"更新 tkinter 小部件参数

python - 扭曲连接丢失事件

python - 2 个扭曲的 SSL 证书

python - 在 Python 中同时启动两个方法

python - 从第二个元素开始对python中的列表进行排序

python - 当电子邮件正文中包含中文字符时,email.retr 检索奇怪的 =20 个字符

python - 从 Twisted 中的 SSL 套接字读取