You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python Tkinter报错NameError: name 'delete' is not defined求助

问题解析与修复方案

核心错误:NameError: name 'delete' is not defined

Python是逐行解释执行的语言,你在创建按钮b1时指定了command=delete,但此时delete函数的定义在按钮代码的后面。解释器执行到按钮这一行时,还未加载delete函数的定义,因此抛出"未定义"的错误。

修复方法

将delete、nextpage、save三个函数的定义,移到创建按钮的代码之前,确保解释器在处理按钮的command参数时,已经识别这些函数的存在。

其他潜在问题(避免后续报错)

  • 控件变量赋值错误:你创建控件时直接链式调用.place(),比如labelmes = Label(windows, text="Mese").place(x=110, y=60),这会导致labelmes存储的是None(place()方法返回值为None),后续nextpage函数中调用labelmes.destroy()会抛出AttributeError: 'NoneType' object has no attribute 'destroy'。正确写法是分开赋值与布局:

    labelmes = Label(windows, text="Mese")
    labelmes.place(x=110, y=60)
    

    所有控件(Label、Combobox、Button等)都需要按此修改,包括b1、b2、b3。

  • 重复变量覆盖:你多次使用textbox变量,比如:

    textbox = Text(windows, width=17, height=1).place(x=200, y=90)
    ...
    textbox = Text(windows, width=17, height=1).place(x=200, y=120)
    

    这会覆盖之前的textbox引用,若后续需要操作这些Text控件,会无法找到对应实例,建议给每个Text控件设置唯一变量名。

  • 导入缺失:代码中用到了Combobox(属于ttk)、showinfo、Workbook、pd(pandas),需要在代码开头补充导入:

    import tkinter as tk
    from tkinter import ttk
    from tkinter.messagebox import showinfo
    from openpyxl import Workbook
    import pandas as pd
    
  • showinfo参数缺失:原代码中showinfo("Hai salvato correttamente")缺少标题参数,showinfo需要至少两个参数,应修改为showinfo("提示", "Hai salvato correttamente")。

修复后的核心代码片段示例

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from openpyxl import Workbook
import pandas as pd

# 先定义所有函数
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 = tk.Tk()
windows.geometry("400x350")
windows.title("Gym Tracker")
windows.resizable(False, False)

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

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

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

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

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

labelmgrassa = tk.Label(windows, text="Massa Grassa")
labelmgrassa.place(x=110, y=180)
textbox4 = tk.Text(windows, width=17, height=1)
textbox4.place(x=200, y=180)
entry6 = tk.Entry(windows)
entry6.place(x=200, y=180)

labelutente = tk.Label(windows, text="Utente")
labelutente.place(x=110, y=210)
combobox2 = ttk.Combobox(windows, values=['Erika', 'Lorenzo'])
combobox2.place(x=200, y=210)
entry7 = tk.Entry(windows)
entry7.place(x=200, y=210)

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

windows.mainloop()

内容的提问来源于stack exchange,提问作者snorky

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 08:25:17