I would like some help making a backspace button function where when pressed it removes and clears the last number inputted for example if i have 561 and i press the button it clears the 1 and shows 56 if anyone could help me make this backspace function it would be great and highly appreciated. Everything is formated and fine all i need is the backspace to work so when i press it the last number showing in the top display clears
from tkinter import *
from math import *
root = Tk()
root.title("Calculator")
root.resizable(width=False, height=False)
screen = StringVar()
screen.set("0")
current = ""
power = ""
expression = ""
firstnum = str()
secondnum = str()
mathsign = str()
defxworking = False
percentt = False
def math_button_pressed():
if mathsign == " ":
button_plus.config(relief=SUNKEN)
if mathsign == "-":
button_minus.config(relief=SUNKEN)
if mathsign == "*":
button_multiply.config(relief=SUNKEN)
if mathsign == "/":
button_division.config(relief=SUNKEN)
def math_button_raised():
button_plus.config(relief=RAISED)
button_minus.config(relief=RAISED)
button_multiply.config(relief=RAISED)
button_division.config(relief=RAISED)
def is_int(num):
if int(num) == float(num):
return int(num)
else:
return float(num)
def number_pressed(butt):
global current, power, firstnum, secondnum
if mathsign == str() and defxworking == False:
current = current str(butt)
screen.set(current)
firstnum = float(current)
elif mathsign != str() and defxworking == False:
math_button_raised()
current = current str(butt)
screen.set(current)
secondnum = float(current)
elif mathsign == str() and defxworking == True:
power = power str(butt)
current = current str(butt)
screen.set(current)
elif mathsign != str and defxworking == True:
power = power str(butt)
current = current str(butt)
screen.set(current)
print(power)
def math_pressed(math):
global current, power, mathsign, firstnum, secondnum, defxworking, percentt
if (
mathsign == str()
and defxworking == False
and percentt == False
and firstnum != str()
):
mathsign = str(math)
math_button_pressed()
current = ""
elif mathsign != str() and defxworking == False and percentt == False:
print(2)
if mathsign == " ":
firstnum = round(float(firstnum secondnum), 6)
if mathsign == "-":
firstnum = round(float(firstnum - secondnum), 6)
if mathsign == "*":
firstnum = round(float(firstnum * secondnum), 6)
if mathsign == "/":
firstnum = round(float(firstnum / secondnum), 6)
screen.set(is_int(firstnum))
mathsign = str(math)
math_button_pressed()
current = ""
elif mathsign != str() and defxworking == True and percentt == False:
if mathsign == " ":
firstnum = round(firstnum secondnum ** int(power), 6)
if mathsign == "-":
firstnum = round(firstnum - secondnum ** int(power), 6)
if mathsign == "*":
firstnum = round(firstnum * (secondnum ** int(power)), 6)
if mathsign == "/":
firstnum = round(firstnum / (secondnum ** int(power)), 6)
defxworking = False
screen.set(is_int(firstnum))
defxworking = False
mathsign = str(math)
math_button_pressed()
power = ""
current = ""
elif defxworking and percentt == False:
firstnum = round(firstnum ** int(power), 6)
defxworking = False
screen.set(is_int(firstnum))
mathsign = str(math)
math_button_pressed()
power = ""
current = ""
elif percentt:
if mathsign == " ":
firstnum = round(float(firstnum firstnum / 100 * secondnum), 6)
if mathsign == "-":
firstnum = round(float(firstnum - firstnum / 100 * secondnum), 6)
screen.set(is_int(firstnum))
percentt = False
mathsign = str(math)
math_button_pressed()
current = ""
def squareroot():
global firstnum, secondnum, mathsign, current
if mathsign == str():
firstnum = round(sqrt(firstnum), 6)
screen.set(is_int(firstnum))
if mathsign != str():
if mathsign == " ":
firstnum = round(sqrt(firstnum float(secondnum)), 6)
if mathsign == "-":
firstnum = round(sqrt(firstnum - float(secondnum)), 6)
if mathsign == "*":
firstnum = round(sqrt(firstnum * float(secondnum)), 6)
if mathsign == "/":
firstnum = round(sqrt(firstnum / float(secondnum)), 6)
screen.set(is_int(firstnum))
secondnum = str()
mathsign = str()
current = ""
def x():
global firstnum, secondnum, mathsign, current, defxworking
if mathsign == str():
current = str(is_int(firstnum)) "^"
screen.set(current)
defxworking = True
elif mathsign != str():
current = str(is_int(secondnum)) "^"
screen.set(current)
defxworking = True
def result():
global firstnum, secondnum, mathsign, current, power, defxworking, percentt
if defxworking == False and percentt == False:
if mathsign == " ":
firstnum = round(float(firstnum secondnum), 6)
if mathsign == "-":
firstnum = round(float(firstnum - secondnum), 6)
if mathsign == "*":
firstnum = round(float(firstnum * secondnum), 6)
if mathsign == "/":
firstnum = round(float(firstnum / secondnum), 6)
screen.set(is_int(firstnum))
if mathsign == str() and defxworking == True and percentt == False:
firstnum = round(firstnum ** int(power), 6)
defxworking = False
screen.set(is_int(firstnum))
if mathsign != str() and defxworking == True and percentt == False:
if mathsign == " ":
firstnum = round(firstnum secondnum ** int(power), 6)
defxworking = False
if mathsign == "-":
firstnum = round(firstnum - secondnum ** int(power), 6)
defxworking = False
if mathsign == "*":
firstnum = round(firstnum * (secondnum ** int(power)), 6)
defxworking = False
if mathsign == "/":
firstnum = round(firstnum / (secondnum ** int(power)), 6)
defxworking = False
screen.set(is_int(firstnum))
if defxworking == False and percentt == True:
if mathsign == " ":
firstnum = round(float(firstnum firstnum / 100 * secondnum), 6)
screen.set(is_int(firstnum))
percentt = False
if mathsign == "-":
firstnum = round(float(firstnum - firstnum / 100 * secondnum), 6)
screen.set(is_int(firstnum))
percentt = False
mathsign = str()
current = ""
power = ""
if defxworking == False and mathsign == "*" or "/" and percentt == True:
clear()
def clear():
global current, firstnum, secondnum, mathsign, power, defxworking, percentt
screen.set(0)
current = ""
power = ""
firstnum = str()
secondnum = str()
mathsign = str()
defxworking = False
math_button_raised()
percentt = False
def percent():
global firstnum, secondnum, current, percentt
current = str(is_int(secondnum)) "%"
screen.set(current)
percentt = True
def press(num):
if button_toggle.config("text")[-1] == "ON":
global expression
expression = expression str(num)
equation.set(expression)
def equalpress():
try:
global expression
total = str(eval(expression))
equation.set(total)
expression = ""
except:
equation.set(" error ")
expression = ""
def clear():
global expression
expression = ""
equation.set("")
def Simpletoggle():
if button_toggle.config("text")[-1] == "ON":
button_toggle.config(text="OFF")
for button in all_buttons:
button.config(state="normal")
screen.set(current)
else:
button_toggle.config(text="ON")
for button in all_buttons:
button.config(state="disabled")
screen.set("")
def clear():
global current, firstnum, secondnum, mathsign, power, defxworking, percentt
screen.set(0)
current = ""
power = ""
firstnum = str()
secondnum = str()
mathsign = str()
defxworking = False
math_button_raised()
percentt = False
def __init__(self):
self.total = 0
self.current = ""
self.new_num = True
self.op_pending = False
self.op = ""
self.eq = False
def backspace(self):
self.current = self.current[:-1]
if self.current == "":
self.new_num = True
self.current = "0"
calculation = Entry(root, textvariable=screen, font=("Verdana", 15, ), bd=12, insertwidth=4, width=14, justify=RIGHT,)
calculation.grid(columnspan=4)
button1 = Button(root, text="1", command=lambda: number_pressed(1), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button1.grid(row=2, column=0, sticky=W)
button2 = Button(root, text="2", command=lambda: number_pressed(2), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button2.grid(row=2, column=1, sticky=W)
button3 = Button(root, text="3", command=lambda: number_pressed(3), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button3.grid(row=2, column=2, sticky=W)
button4 = Button(root, text="4", command=lambda: number_pressed(4), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button4.grid(row=3, column=0, sticky=W)
button5 = Button(root, text="5", command=lambda: number_pressed(5), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button5.grid(row=3, column=1, sticky=W)
button6 = Button(root, text="6", command=lambda: number_pressed(6), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button6.grid(row=3, column=2, sticky=W)
button7 = Button(root, text="7", command=lambda: number_pressed(7), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button7.grid(row=4, column=0, sticky=W)
button8 = Button(root, text="8", command=lambda: number_pressed(8), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button8.grid(row=4, column=1, sticky=W)
button9 = Button(root, text="9", command=lambda: number_pressed(9), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button9.grid(row=4, column=2, sticky=W)
button0 = Button(root, text="0", command=lambda: number_pressed(0), bg="gainsboro",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button0.grid(row=5, column=0, sticky=W)
button_float = Button(root, text=".", command=lambda: number_pressed("."), bg="gainsboro",
bd=3, padx=15, pady=5, font=("Helvetica", 14, "bold"))
button_float.grid(row=5, column=1)
button_plus = Button(root, text=" ", command=lambda: math_pressed(" "), bg="gray",
bd=3, padx=11, pady=5, font=("Helvetica", 14, "bold"))
button_plus.grid(row=2, column=3, sticky=W)
button_minus = Button(root, text="-", command=lambda: math_pressed("-"), bg="gray",
bd=3, padx=11, pady=4, font=("Verdana", 14, "bold"))
button_minus.grid(row=3, column=3, sticky=W)
button_multiply = Button(root, text="*", command=lambda: math_pressed("*"), bg="gray",
bd=3, padx=13, pady=5, font=("Helvetica", 14, "bold"))
button_multiply.grid(row=4, column=3)
button_division = Button(root, text="/", command=lambda: math_pressed("/"), bg="gray",
bd=3, padx=14, pady=5, font=("Helvetica", 14, "bold"))
button_division.grid(row=5, column=3)
button_equal = Button(root, text="=", command=lambda: result(), bg="gray70",
bd=3, padx=12, pady=5, font=("Arial", 14))
button_equal.grid(row=5, column=2)
button_percent = Button(root, text="%", command=lambda: percent(), bg="gray",
bd=3, padx=8, pady=5, font=("Helvetica", 14, "bold"))
button_percent.grid(row=1, column=3)
button_toggle = Button(root, text="OFF", command=Simpletoggle, bg="gray",
bd=3, padx=6, pady=6, font=("Helvetica", 12))
button_toggle.grid(row=1, column=0)
button_sqrt = Button(root, text="√", command=lambda: squareroot(), bg="gray",
bd=3, padx=12, pady=5, font=("Helvetica", 14, "bold"))
button_sqrt.grid(row=1, column=2, sticky=W)
button_x = Button(root, text="x^y", command=lambda: x(), bg="gray",
bd=3, padx=6, pady=5, font=("Helvetica", 14))
button_x.grid(row=1, column=1, sticky=W)
button_clear = Button(root, text='CLEAR', command=lambda: clear(), bg='gray',
bd=3, padx=12, pady=5, font=("Helvetica", 16))
button_clear.grid(row=6, columnspan=2)
button_backspace = Button(root, text='BACK', command=lambda: backspace() , bg='gray',
bd=3, padx=15, pady=5, font=("Helvetica", 16))
button_backspace.grid(row=6, column=2, columnspan=2)
all_buttons = [
button1,
button2,
button3,
button4,
button5,
button6,
button7,
button8,
button9,
button0,
button_float,
button_plus,
button_minus,
button_multiply,
button_division,
button_equal,
button_percent,
button_sqrt,
button_x,
button_clear,
button_backspace,
]
root.mainloop()
CodePudding user response:
hello davoz this my code write for you :
from tkinter import *
def back():
y = len(entry.get())
entry.delete(y-1)
root = Tk() root.geometry("800x600")
entry = Entry(root) entry.pack()
button = Button(root,text="back",command=back).pack()
mainloop()
CodePudding user response:
Your problem has been solved. What I did was add the calculation.get()
and then substract minus 1. ex[0:len(ex) - 1]
And do not used lambada
in command button.
Here is snipped code:
def backspace():
ex = calculation.get()
ex = ex[0:len(ex) - 1]
calculation.delete(0, END)
calculation.insert(0, ex)
button_backspace = Button(root, text='BACK', command=backspace, bg='gray',
bd=3, padx=15, pady=5, font=("Helvetica", 16))