简体   繁体   English

Python - AttributeError: 'str' object 没有属性 'destroy'

[英]Python - AttributeError: 'str' object has no attribute 'destroy'

I'm completely new and learning Python.我是全新的,正在学习 Python。

I'm getting the error in title.我收到标题错误。 Should I convert the "str" to StringVar()?我应该将“str”转换为 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.谢谢你,对不起“肮脏”的代码,我几周前就开始学习了。

Your error is on line 70. Line 70 itself is unneeded because you can simply reassign errorempty to the new object.您的错误在第 70 行。第 70 行本身是不需要的,因为您可以简单地将errorempty重新分配给新的 object。 To fix your problem, delete line 70.要解决您的问题,请删除第 70 行。

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)

The way your code is set up now, errorempty is always equal to "" when errorempty.destroy() is executed.现在设置代码的方式, errorempty在执行errorempty.destroy()时始终等于"" "" is a string, not at tkinter widget, so it does not have a destroy method. ""是一个字符串,不在 tkinter 小部件中,所以它没有销毁方法。 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:一般来说,如果您需要在销毁 object 之前检查它是否存在,您可以使用以下方法:

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 .请注意,最好将errorempty=""更改为errorempty = None因为它应该包含Label ,而不是str But in this case you do not need either of those lines, since you assign errorempty inside the if-statement.但在这种情况下,您不需要这两行中的任何一行,因为您在 if 语句中分配errorempty


Also consider heading over to Code Review once your code is working correctly, to get some tips about improving your code.还可以考虑在您的代码正常工作后前往Code Review ,以获取有关改进代码的一些提示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM