Don't understand this AttributeError:'str' object has no attribute 'text'

17,179
AttributeError:'str' object has no attribute 'text', tkinter

This means that somewhere in your code, you have a str object that you are trying to call the .text() method of it on.
Because your str object does not have a .text() method, you get that error.

To resolve this, check your variable types, you should not be using a str object, but rather an object that has .text() method

Share:
17,179

Related videos on Youtube

Philip Vasilevski
Author by

Philip Vasilevski

Updated on June 04, 2022

Comments

  • Philip Vasilevski
    Philip Vasilevski almost 2 years

    I am making an application that will allow to display a running text in the window, but just started to parse the OOP, and I would like to know how to fix this error... There is also an example that works as needed, it will be shown below the script with an error.

    class Main_Desktop():
    
        def __init__(self,parent,i,text):
            self.i=i
            self.parent=parent
            self.ticker=Text(parent,height=1,width=100)
            self.text=text
            self.ticker.pack()
            self.txt(i)
    
        def txt(self, i):
            i = 0
            self.text = ('' * 20) + self.text + ('' * 20)
            x = self.text[i:i + 20]
            self.ticker.insert("1.1", x)
            if i == len(self.text):
                i = 0
            else:
                i = i + 1
            self.ticker.after(100, lambda: Main_Desktop.txt(self.text[i:i + 20], i))
    

    Here is an example and it works as needed:

    root =Tk()
    text="string"
    text = (' '*20) + text + (' '*20)
    ticker = Text(root, height=1, width=20)
    ticker.pack()
    
    i = 0
    def command(x, i):
        ticker.insert("1.1", x)
        if i == len(text):i = 0
        else:i = i+1
        root.after(100, lambda:command(text[i:i+20], i))
    
    command(text[i:i+20], i)
    
    • ycx
      ycx over 5 years
      Which line generates this error?
    • hpaulj
      hpaulj over 5 years
      What's the point to asking us about an error without any information about where it occurs (e.g. the traceback). Sometimes we can fetch that information by running your code, but I doubt if your example copy-n-paste runnable.
  • Philip Vasilevski
    Philip Vasilevski over 5 years
    Thanks, it helped, but the application does not work as it should. Could you tell me why I get the result of displaying windows vertically, when the window should be one and the text in it should run:def __init__(self,parent,i=0):self.i=I self.parent=parent self.x=Main_Desktop.text[i:i+20] self.txt(self.x,i) def txt(self,x,i=0): text='abc' text = (' '* 20) + text + (20*'') x = text[i:i+20] ticker=Text(root,height=1,width=20) ticker.pack() ticker.insert("1.1", x) if i == len(text):i = 0 else:i = i + 1 root.after(100, lambda: Main_Desktop.txt(x,i))
  • ycx
    ycx over 5 years
    @PhilipVasilevski It is very difficult to read it like this, plus I don't necessarily have the expertise on tkinter. Perhaps you should open another question. If this post helped you solve your initial problem, please help mark it as the answer, thanks!