Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python-AttributeError:';str';对象没有属性';摧毁';_Python_String - Fatal编程技术网

Python-AttributeError:';str';对象没有属性';摧毁';

Python-AttributeError:';str';对象没有属性';摧毁';,python,string,Python,String,我是全新的,正在学习Python 我得到了标题上的错误。 我应该将“str”转换为StringVar()吗?如果是,我怎么做 下面是代码中有问题的部分: def count(): print(user_entries[2].get()) errorempty="" g=0 h=0 while g<nbofcalc: if user_entries[g].get

我是全新的,正在学习Python

我得到了标题上的错误。 我应该将“str”转换为StringVar()吗?如果是,我怎么做

下面是代码中有问题的部分:

    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)

谢谢你,很抱歉“脏”代码,我几周前开始学习。

你的错误在第70行。不需要第70行本身,因为您可以简单地将
errorempty
重新分配给新对象。要解决问题,请删除第70行。

问题是您试图销毁一个字符串,但这是无法完成的。像这样修复它:

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)
def count():
打印(用户\u条目[2]。获取()
#删除此>>>errorempty=“”
g=0
h=0
而g>>和this if不是全局变量,则为errorempty.destroy()
errorempty=标签(text=“下载将开始”,fg=“红色”)
errorempty.pack(侧面=底部)
boutoncount=按钮(fenetre,text=“count”,width=800,height=1,bg=“white”,font=“bold”,bd=5,command=count)
boutoncount.pack(侧面=底部)

现在设置代码的方式是,执行
errorempty.destroy()
时,
errorempty
始终等于
”<代码>“是字符串,而不是tkinter小部件,因此它没有销毁方法。因此,您当前不需要该行

通常,如果需要在销毁对象之前检查对象是否存在,可以使用以下方法:

if errorempty is not None:
    errorempty.destroy()
请注意,最好将
errorempty=”“
更改为
errorempty=None
,因为它应该持有
标签,而不是
str
。但是在这种情况下,您不需要这两行中的任何一行,因为您在if语句中指定了
errorempty



也要考虑到你的代码工作正常,得到一些关于改进代码的提示。< /P>如果用户没有输入任何输入,那么就从标签中输入错误信息:“你没有输入计算数字……”。但是如果他在这条消息后面输入了信息,那么就会显示“下载将开始”标签。问题是我需要删除第一个标签“You have enter NO…”。否则,新消息将出现在上面。消息从一开始就不会显示。这就是if-else语句的工作原理。如果且仅当布尔条件为true,则将采用第一条路径,否则将采用

else
路径。请添加您试图抽象执行的操作,以便为您提供更高效的代码。干杯,谢谢。共有9个条目。用户可以输入9个计算编号,以便下载单据。h=用户输入的计算数。要启动下载,他必须点击一个按钮。(1) 当他点击它时,如果他没有输入任何数字=错误消息:“你没有输入任何计算数字”。(2) 因此,他仍然可以输入新的计算编号并再次单击,然后出现消息“下载将开始”。问题:(1)的消息保留,来自(2)的消息位于消息上方。我希望(1)消失,让消息(2)单独显示。
if errorempty is not None:
    errorempty.destroy()