1

I'm quite a new programmer and I would be appreciated if you could help me. I have looked on the google but there are a few examples with "Twisted+Tkinter". When i click "Send" button on the window i get this error:

The error is e.insert(0,m) AttributeError: 'str' object has no attribute 'insert'

And the code:

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()
4
  • 1
    What do you expect to happen when you call insert()? You are manipulating a label string as if it is a mutable list, but a str is not a mutable sequence type. Commented Feb 28, 2013 at 12:41
  • I'am trying to change the text on the label "labeltext" with the entry "entryvar" Commented Feb 28, 2013 at 12:43
  • For what it's worth this seems to have nothing to do with Twisted. Commented Mar 1, 2013 at 8:37
  • I am just testing it, if i could solve this problem i'll apply it on twisted Commented Mar 1, 2013 at 8:41

2 Answers 2

3

If you want to update the labeltext StringVar, you need to construct a new string value for it. str is an immutable sequence type, and unlike list cannot be modified in-place.

In this case, perhaps you meant to use string concatenation? The following code will set labeltext to m + e, where e is the previous contents of labeltext, effectively prepending m:

def onButton(self):
    m=self.entryvar.get()
    e=self.labeltext.get()
    self.labeltext.set(m + e)
Sign up to request clarification or add additional context in comments.

6 Comments

Thank you, the error is disappeared but nothing's happened on the window. Any idea?
@billwild: Does entryvar have a value? If it is empty, then labeltext will appear unchanged. You could try debugging this by changing the .set() call to self.labeltext.set('m: ' + m + ', e: ' + e), for example.
Should i make self.entryvar=StringVar() /n self.entryvar.set("something")
@billwild: No, why would you need to do that? You are merely reading entryvar.
I don't know what i'm doing, please just instruct me.
|
1

If you want to replace Label text with Entry text

 def onButton(self):
    entry_text = self.entryvar.get()
    self.labeltext.set(entry_text)

Or if you want to add entry text before label text

 def onButton(self):
    entry_text = self.entryvar.get()
    label_text = self.labeltext.get()
    self.labeltext.set(entry_text + " " + label_text)

1 Comment

The error is disappeared but nothing's happened on the window

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.