【问题标题】:NameError: name 'delete' is not definedNameError: name \'delete\' 未定义
【发布时间】:2022-09-23 16:56:22
【问题描述】:

你好我最近一直在学习 python 并且正在练习......我只是无法理解错误的原因,因为在我看来,我在按钮内部定义。 如果有人可以帮助我,我将不胜感激

我不知道该怎么继续了,我不明白错误在哪里,我想能够理解但我做不到,你能给我解释一下吗?


windows = tk.Tk()
windows.geometry(\"400x350\")
windows.title(\"Gym Tracker\")
windows.resizable(False, False)

frame1 = Frame(windows, width=150, height=30, highlightcolor=\"white\",highlightbackground=\"black\", highlightthickness=1).place(x=120, y=2)
label1 = Label(windows, text=\"GYM TRACKER\").place(x=150, y=7)
frame2 = Frame(windows, width=500, height=1, highlightcolor=\"white\",highlightbackground=\"black\", highlightthickness=1).place(x=2, y=45)

labelmes = Label(windows, text=\"Mese\").place(x=110, y=60)
combobox = Combobox(windows, values=[\'Gennaio\', \'Febbraio\', \'Marzo\', \'Aprile\', \'Maggio\', \'Giugno\',\'Luglio\', \'Agosto\', \'Settembre\', \'Ottobre\', \'Novembre\', \'Dicembre\']).place(x=200, y=60)
entry2 = tk.Entry(windows)

labealt = Label(windows, text=\"Altezza\").place(x=110, y=90)
textbox = Text(windows, width=17, height=1).place(x=200, y=90)
entry3 = tk.Entry(windows)

labelpeso = Label(windows, text=\"Peso\").place(x=110, y=120)
textbox = Text(windows, width=17, height=1).place(x=200, y=120)
entry4 = tk.Entry(windows)

labelmmagra = Label(windows, text=\"Massa Magra\").place(x=110, y=150)
textbox = Text(windows, width=17, height=1).place(x=200, y=150)
entry5 = tk.Entry(windows)

labelmgrassa = Label(windows, text=\"Massa Grassa\").place(x=110, y=180)
textbox = Text(windows, width=17, height=1).place(x=200, y=180)
entry6 = tk.Entry(windows)
# Combobox Utente
labelutente = Label(windows, text=\"Utente\").place(x=110, y=210)
combobox = Combobox(windows, values=[\'Erika\', \'Lorenzo\']).place(x=200, y=210)
entry7 = tk.Entry(windows)


b1 = tk.Button(windows, text=\"Importa\", command=delete ,width=8,`enter code here`height=1).place(x=170,y=310)
b2 = tk.Button(windows, text=\"Avanti\", width=8,height=1,command=nextpage).place(x=300,y=310)
b3 = tk.Button(windows, text=\'Salva\', width=8, height=1, command=save).place(x=50, y=310)

def delete():
    entry2.delete(0, tk.END)
    entry3.delete(0, tk.END)
    entry4.delete(0, tk.END)
    entry5.delete(0, tk.END)
    entry6.delete(0, tk.END)
    entry7.delete(0, tk.END)

def nextpage():
    labelmes.destroy()
    labealt.destroy()
    labelpeso.destroy()
    labelmmagra.destroy()
    labelmgrassa.destroy()
    labelutente.destroy()
    b1.destroy()
    b2.destroy()
    b3.destroy()
    
def save():
    mese = entry2.get()
    altezza = entry3.get()
    peso = entry4.get()
    mmagra = entry5.get()
    mgrassa = entry6.get()
    utente = entry7.get()
    wb = Workbook()
    ws = wb.active
    ws[\'A1\'] = \"Mese\"
    ws[\'B1\'] = \"Altezza\"
    ws[\'C1\'] = \"Peso\"
    ws[\'D1\'] = \"Massa Magra\"
    ws[\'E1\'] = \"Massa Grassa\"
    ws[\'F1\'] = \"Utente\"
    ws[\'A2\'] = mese
    ws[\'B2\'] = altezza
    ws[\'C2\'] = peso
    ws[\'D2\'] = mmagra
    ws[\'E2\'] = mgrassa
    ws[\'F2\'] = utente
    wb.save(r\'C:\\Users\\lricci\\Desktop\\SERVER\\web\\Gym Tracker\\Gym Tracker v1.0\\track.xlsx\')
    showinfo(\"Hai salvato correttamente\")
    file = pd.read_excel(\"track.xlsx\")
    all = [file]
    append = pd.concat(all)
    append.to_excel(\"track.xlsx\",index=False)
windows.mainloop()

    标签: python-3.x


    【解决方案1】:

    这条线

    b1 = tk.Button(windows, text="Importa", command=delete ,width=8,`enter code here`height=1).place(x=170,y=310)
    

    将按钮附加到 delete 函数,但是,在您执行它时,您还没有定义删除功能(稍后您会这样做)。

    尝试将该行移到您的 def delete(): 块下方。

    【讨论】:

    • 非常感谢,所以只是定位错误
    • 这是我从提供的信息中最好的猜测。异常应该给你一个导致异常的行号。如果这是我引用的那句话,那几乎肯定是问题所在。如果是另一条线,您可能会遇到多个问题。但从根本上说,Python 在第一遍就完成了所有事情,不像某些编译语言(C#/Java)对文件进行多次遍历,这意味着文件中方法的位置远没有那么重要。
    猜你喜欢
    • 2022-11-21
    • 2022-11-25
    • 2016-05-24
    • 1970-01-01
    • 2023-01-17
    • 2020-11-15
    • 1970-01-01
    • 2023-01-30
    • 2023-02-17
    相关资源
    最近更新 更多