Meriole
Meriole

Reputation: 115

Python - AttributeError: 'str' object has no attribute 'destroy'

I'm completely new and learning Python.

I'm getting the error in title. Should I convert the "str" to StringVar() ? If yes, how could I do it ?

Here is the problematic part of the code:

    def count():
        print(user_entries[2].get())
        errorempty=""
        g=0
        h=0
        while g<nbofcalc:
            if user_entries[g].get() !="":
                h+=1
            else:
                h+=0
            g+=1
        print(h)
        hstr=str(h)
        if h==0:
            errorempty=Label(text="You have enter NO calculation number, please enter at least one", fg="red")
            errorempty.pack(side=BOTTOM)
        else:
            errorempty.destroy()
            errorempty=Label(text="Download  will start", fg="red")
            errorempty.pack(side=BOTTOM)
        
        
    boutoncount=Button(fenetre,text="count",width=800,height=1,bg="white", font="bold",bd=5, command=count)
    boutoncount.pack(side=BOTTOM)

Here is the error message:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\Marcha02\AppData\Local\Programs\Python\Python39\lib\tkinter\__init__.py", line 1884, in __call__
    return self.func(*args)
  File "C:\Users\Marcha02\Desktop\Python\Tkinter - GUI 3 - Entries with Loop.py", line 70, in count
    errorempty.destroy()
AttributeError: 'str' object has no attribute 'destroy'

Thank you and sorry the "dirty" code, I started to learn some weeks ago.

Upvotes: 0

Views: 1047

Answers (3)

riskypenguin
riskypenguin

Reputation: 2199

The way your code is set up now, errorempty is always equal to "" when errorempty.destroy() is executed. "" is a string, not at tkinter widget, so it does not have a destroy method. You therefore currently don't need that line.

In general, if you need to check if the object exists before destroying it, you could use the following:

if errorempty is not None:
    errorempty.destroy()

Note that it's better to change errorempty="" to errorempty = None since it's supposed to hold a Label, not a str. But in this case you do not need either of those lines, since you assign errorempty inside the if-statement.


Also consider heading over to Code Review once your code is working correctly, to get some tips about improving your code.

Upvotes: 0

miquelvir
miquelvir

Reputation: 1757

The issue is that you are trying to destroy a string, which can't be done. Fix it like this:

def count():
    print(user_entries[2].get())
    # remove this >>> errorempty=""
    g=0
    h=0
    while g<nbofcalc:
        if user_entries[g].get() !="":
            h+=1
        else:
            h+=0
        g+=1
    print(h)
    hstr=str(h)
    if h==0:
        errorempty=Label(text="You have enter NO calculation number, please enter at least one", fg="red")
        errorempty.pack(side=BOTTOM)
    else:
        # >>> and this if it is not a global variable too errorempty.destroy()
        errorempty=Label(text="Download  will start", fg="red")
        errorempty.pack(side=BOTTOM)
    
    
boutoncount=Button(fenetre,text="count",width=800,height=1,bg="white", font="bold",bd=5, command=count)
boutoncount.pack(side=BOTTOM)

Upvotes: 0

101donutman
101donutman

Reputation: 80

Your error is on line 70. Line 70 itself is unneeded because you can simply reassign errorempty to the new object. To fix your problem, delete line 70.

Upvotes: 1

Related Questions