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

Time:05-06

I made a calculator and I made it memorize in memory the first_number/F_NUM, and when I tried to use a function I made which is called: clear() to kind of delete the F_NUM from memory it gave me this error: (cannot delete function call).

#sorry for the messy code but if you can help me please do! here is my code:

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()


CodePudding user response:

del globals(F_NUM)

globals is function, globals(F_NUM) is function call, del statement should be followed by target. In your case variable name, if you want to del something from globals dict using function then first use global variable name followed by del statement for example:

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