Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- root = Tk()
- root.resizable(0,0)
- root['bg'] = '#1e2936'
- texts = [
- 'DEL', 'C', 'x²', '√',
- '7', '8', '9', '+',
- '4', '5', '6', '-',
- '1', '2', '3', 'x',
- '.', '0', '=', '÷'
- ]
- def add(num):
- global first, operation
- first = num
- e.delete(0,'end')
- operation = 'add'
- return 'break'
- def sub(num):
- global first, operation
- first = num
- e.delete(0,'end')
- operation = 'sub'
- return 'break'
- def mult(num):
- global first, operation
- first = num
- e.delete(0,'end')
- operation = 'mult'
- return 'break'
- def div(num):
- global first, operation
- first = num
- e.delete(0,'end')
- operation = 'div'
- return 'break'
- def equals():
- final = e.get()
- e.delete(0,'end')
- if operation == 'add':
- e.insert('end',int(first)+int(final))
- if operation == 'sub':
- e.insert('end',int(first)-int(final))
- if operation == 'mult':
- e.insert('end',int(first)*int(final))
- if operation == 'div':
- e.insert('end',int(int(first)/int(final)))
- def evaluator(btn):
- if btn.cget('text') in '1234567890.':
- e.insert('end',btn.cget('text'))
- if btn.cget('text') in ('+', '-', 'x', '÷', '√', 'x²'):
- if btn.cget('text') == '+':
- add(e.get())
- if btn.cget('text') == '-':
- sub(e.get())
- if btn.cget('text') == 'x':
- mult(e.get())
- if btn.cget('text') == '÷':
- div(e.get())
- if btn.cget('text') in ("C", "DEL"):
- if btn.cget('text') == 'C':
- num = e.get()[:-1]
- e.delete(0,'end')
- e.insert('end',num)
- if btn.cget('text') == 'DEL':
- e.delete(0,'end')
- if btn.cget('text') == '=':
- equals()
- e.focus()
- def validation(inp):
- if inp.isdigit():
- return True
- elif inp == '':
- return True
- else:
- return False
- reg = root.register(validation)
- e = Entry(root,fg='white',font=('',24,'bold'),bg='#1e2936',validatecommand=(reg,'%P'),validate='key',bd=0,justify='right')
- e.grid(row=0,column=0,columnspan=4,sticky='news',pady=10,padx=5)
- e.focus()
- btns = []
- img = PhotoImage(width=1,height=1)
- for i in range(5):
- for j in range(4):
- text = texts[4*i+j]
- if text in '1234567890.': color = "#10B981"
- elif text in ('+', '-', 'x', '÷', '√', 'x²'): color = "#F59E0B"
- elif text in "=": color = "#047857"
- elif text in ("C", "DEL"): color = "#2563EB"
- btn = Button(root,text=text,fg='white',image=img,compound='t',width=100)
- btn.config(height=100,relief='flat',font=('',18,'bold'),bg=color)
- btn.config(command=lambda i=btn: evaluator(i))
- btns.append(btn)
- btns[-1].grid(row=i+1,column=j,padx=5,pady=5)
- root.bind('<plus>',lambda event: add(e.get()))
- root.bind('<minus>',lambda event: sub(e.get()))
- root.bind('<asterisk>',lambda event: mult(e.get()))
- root.bind('<slash>',lambda event: div(e.get()))
- # root.bind('<equal>',lambda event: equals())
- root.bind('<Return>',lambda event: equals())
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement