在我的代码中,我制作了一个 clear() 函数作为计算器中的普通清除按钮,但是有一个问题

In my code I made a clear() function to act as a normal clear button in my calculator but there is a problem

我制作了一个计算器,并让它在内存中记住 first_number/F_NUM,当我尝试使用我制作的函数时,它被称为:clear() to kind从内存中删除 F_NUM 它给了我这个错误: (cannot delete function call).

#抱歉代码乱七八糟,但如果你能帮助我,请帮助我! 这是我的代码:

from cProfile import label
from tkinter import *
from tkinter import ttk
import tkinter.font as font

root = Tk()
root.title('Calculator')
root.geometry('300x400')

canvas = Canvas(root, width=300, height=400)
canvas.pack()

Helvetica_fornt = font.Font(family='Helvetica')

#-Functions of buttons-------------------------------------#


def button_click(number):
    current = entry_1.get()
    entry_1.delete(0, END)
    entry_1.insert(0, current + number)


def clear():
    entry_1.delete(0, END)
    del globals(F_NUM) ##########Problem is here############
#ps comment this problem out to try the claculator

def Back_Space():
    entry_1.delete(0, 1)


def addition():
    first_number = entry_1.get()
    global F_NUM
    F_NUM = int(first_number)
    entry_1.delete(0, END)


def equals():
    second_number = entry_1.get()
    entry_1.delete(0, END)
    entry_1.insert(0, F_NUM + int(second_number))
#---------------------------------------------------------#


#---Buttons------------------------------------------------------#


# ps we only need lambda when there are parameters.
button_0 = Button(root, text='0', width=4, height=2,
                  command=lambda: button_click('0'))
button_0.place(x=65, y=350, width=65, height=50)

button_1 = Button(root, text='1', width=4, height=2,
                  command=lambda: button_click('1'))
button_1.place(x=0, y=301, width=65, height=50)  # Location

button_2 = Button(root, text='2', width=4, height=2,
                  command=lambda: button_click('2'))
button_2.place(x=65, y=301, width=65, height=50)  # Location

button_3 = Button(root, text='3', width=4, height=2,
                  command=lambda: button_click('3'))
button_3.place(x=130, y=301, width=65, height=50)  # Location

button_add = Button(root, text='+', width=7, height=2,
                    command=addition)
button_add.place(x=195, y=301, width=105, height=50)  # Location

button_clear = Button(root, text='Clear', width=12, height=2,
                      command=clear)
button_clear.place(x=195, y=100, width=105, height=50)  # Location

back_space = Button(root, text='BackSpace', width=12, height=2,
                    command=Back_Space)
back_space.place(x=195, y=51, width=105, height=50)  # Location

equals_button = Button(root, text='=', width=12, height=2,
                       command=equals)
equals_button.place(x=195, y=350, width=105, height=50)  # Location

#-------------------------------------------------------------------#

#-The entry-----------------------------------------------------------#


entry_1 = Entry(root, borderwidth=3, font=Helvetica_fornt)
entry_1.place(x=1, y=1, width=296, height=50)
#---------------------------------------------------------------------#
root.mainloop()


del globals(F_NUM)

globals是函数,globals(F_NUM)是函数调用,del statement后面应该是target。在您的情况下 变量名 ,如果您想 del 使用函数从全局字典中获取某些内容,则首先使用 global 变量名,然后使用 del 语句示例:

def deletex():
    global x
    del x
x = 10
print(x) # 10
deletex()
print(x) # NameError: name 'x' is not defined