import tkinter as tk
from tkinter import *
root=tk.Tk()
root.geometry("720x1320")
root.title('Calculator')
root.resizable(0, 0)
root['background']='#444444'
def bclick(num): global exp exp= exp str(num) input.set(exp)
def bclear(): global exp exp="" input.set("")
def bequal(): global exp result=str(eval(exp)) input.set(result) exp=""
exp=""
input=StringVar()
input_frame = Frame(root, width = 312, height = 100, bd = 0, highlightbackground = "black", highlightcolor = "black", highlightthickness = 1) input_frame.pack(side = TOP)
#label dis=Entry(input_frame,textvariable=input,bg='#cccccc',fg='#000000',justify=RIGHT,font= ("sans-serif 16"))
dis.place(x=0,y=0) dis.pack(ipady=197)
#0 row bC=tk.Button(root,text='C',padx=166,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclear()) bC.place(x=0,y=479)
bX=tk.Button(root,text='X',padx=78,pady=40,fg='#FFFFFF',bg='#d21405',font=('sans-serif, 14'))
bX.place(x=360,y=479)
bdiv=tk.Button(root,text='÷',padx=78,pady=40,fg='#ffffff',font=('sans-serif, 14'),command=lambda:bclick("/"),bg='#1138be') bdiv.place(x=540,y=479)
#1 row done b7=tk.Button(root,text='7',padx=78,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("7")) b7.place(x=0,y=631)
b8=tk.Button(root,text='8',padx=78,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("8")) b8.place(x=180,y=631) b9=tk.Button(root,text='9',padx=78,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("9")) b9.place(x=360,y=631)
bmul=tk.Button(root,text='×',padx=78,pady=40,bg='#1138be',fg='#ffffff',font=('sans-serif, 14'),command=lambda:bclick("*")) bmul.place(x=540,y=631) #2 row
b4=tk.Button(root,text='4',padx=78,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("4")) b4.place(x=0,y=783)
b5=tk.Button(root,text='5',padx=80,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("5")) b5.place(x=180,y=783)
b6=tk.Button(root,text='6',padx=79,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("6")) b6.place(x=360,y=783)
badd=tk.Button(root,text=' ',padx=80,pady=40,bg='#1138be',fg='#ffffff',font=('sans-serif, 14'),command=lambda:bclick(" ")) badd.place(x=540,y=783) #3row
b1=tk.Button(root,text='1',padx=78,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("1")) b1.place(x=0,y=935)
b2=tk.Button(root,text='2',padx=78,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("2")) b2.place(x=180,y=935)
b3=tk.Button(root,text='3',padx=78,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("3")) b3.place(x=360,y=935)
bsub=tk.Button(root,text='-',padx=82,pady=40,bg='#1138be',fg='#ffffff',font=('sans-serif, 14'),command=lambda:bclick("-")) bsub.place(x=540,y=935) #4 row
b0=tk.Button(root,text='0',padx=165,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick("0")) b0.place(x=0,y=1087)
bdot=tk.Button(root,text='.',padx=85,pady=40,bg='#FFFFFF',font=('sans-serif, 14'),command=lambda:bclick(".")) bdot.place(x=360,y=1087)
bans=tk.Button(root,text='=',padx=80,pady=40,bg='#002366',fg='#ffffff',font=('sans-serif, 14'),command=lambda:bequal()) bans.place(x=540,y=1087)
root.mainloop()
CodePudding user response:
The way I would do it is the same way you've done the last buttons instead of using the text='1' or a number just change it to text='backspace'.
As for the logic behind it since you have a global exp variable storing the maths equation created using the calculator you will need to remove the last bit of the equation added. i.e. if the equation was 12 15 17 - 20
you'd need to look through it and remove 20
because that was the last part of the equation. If they hit backspace again you'd need to remove the -
.
So you're looking to create a function called bRemove()
which removes the last part of your equation.
hint : to look at the last character in your string you can access it doing exp[len(exp)-1], you can check if this is a number and if it is you can remove it and look at the next element in the string (exp[len(exp)-2]) and then see if that is also a number.
[
also some general stack overflow advice to help you on your programming journey, it's better to be more specific about the help requested - some background information is nice and if you are going tto paste code you should use the ``` notation which lets you turn code into blocked code like this
block of code
]
CodePudding user response:
Try this in my example.
from tkinter import *
#definging height and width of button.
button_height=1
button_width=3
#defining height and width of screen
screen_width =450
screen_height= 600
root=Tk()
root.title("SIMPLE CALCULATOR")
root.geometry(f"{screen_width}x{screen_height}")
root.maxsize(screen_width,screen_height)
root.minsize(screen_width,screen_height)
root.configure(background="gray")
scvalue=StringVar()
scvalue.set("")
#This defines the function for click event
def click_me(event):
value=event= event.widget.cget("text")
#print(value)
if value == "=":
try:
if scvalue.get().isdigit():
finalvalue= int(scvalue.get())
else:
finalvalue=eval(scvalue.get())
scvalue.set(finalvalue)
screen.update()
except:
scvalue.set(scvalue.get()[:-1])
screen.update()
elif value=="<=": #for backspace
scvalue.set(scvalue.get()[:-1])
screen.update()
elif value =="1/x": #for calculating reciprocal
if scvalue.get().isdigit():
val=int(scvalue.get())
scvalue.set(1/val)
screen.update()
else:
scvalue.set("Unknown error")
elif value== "C": #for clearing the screen.
scvalue.set("")
screen.update()
else:
scvalue.set(scvalue.get() value)
screen.update()
screen= Entry(root,textvar=scvalue,font="lucida 40 bold" ,justify=RIGHT)
screen.pack(fill=X,padx=5,pady=5)
#list of all the inputs
valuelist=["/","%","C","<=","7","8","9","*","4","5","6","-","1","2","3"," ","1/x","0",".","="]
#this loop
for i in range(20):
if i%4 == 0:
frame=Frame(root,bg="gray")
frame.pack()
b=Button(frame,text=valuelist[i],height=button_height,width=button_width,font="lucida 35 bold")
b.bind("<Button-1>",click_me)
b.pack(side=LEFT,padx=3,pady=3)
root.mainloop()