Advertisement
Guest User

Calc PRO MAX

a guest
May 13th, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. from tkinter import *
  2.  
  3. root = Tk()
  4. root.resizable(0,0)
  5. root['bg'] = '#1e2936'
  6. texts = [
  7.     'DEL', 'C', 'x²', '√',
  8.     '7', '8', '9', '+',
  9.     '4', '5', '6', '-',
  10.     '1', '2', '3', 'x',
  11.     '.', '0', '=', '÷'
  12. ]
  13.  
  14. def add(num):
  15.     global first, operation
  16.     first = num
  17.     e.delete(0,'end')
  18.     operation = 'add'
  19.     return 'break'
  20.  
  21. def sub(num):
  22.     global first, operation
  23.     first = num
  24.     e.delete(0,'end')
  25.     operation = 'sub'
  26.     return 'break'
  27.  
  28. def mult(num):
  29.     global first, operation
  30.     first = num
  31.     e.delete(0,'end')
  32.     operation = 'mult'
  33.     return 'break'
  34.  
  35. def div(num):
  36.     global first, operation
  37.     first = num
  38.     e.delete(0,'end')
  39.     operation = 'div'
  40.     return 'break'
  41.  
  42. def equals():
  43.     final = e.get()
  44.     e.delete(0,'end')
  45.     if operation == 'add':
  46.         e.insert('end',int(first)+int(final))
  47.     if operation == 'sub':
  48.         e.insert('end',int(first)-int(final))
  49.     if operation == 'mult':
  50.         e.insert('end',int(first)*int(final))
  51.     if operation == 'div':
  52.         e.insert('end',int(int(first)/int(final)))
  53.    
  54. def evaluator(btn):
  55.     if btn.cget('text') in '1234567890.':
  56.         e.insert('end',btn.cget('text'))
  57.     if btn.cget('text') in ('+', '-', 'x', '÷', '√', 'x²'):
  58.         if btn.cget('text') == '+':
  59.             add(e.get())
  60.         if btn.cget('text') == '-':
  61.             sub(e.get())
  62.         if btn.cget('text') == 'x':
  63.             mult(e.get())
  64.         if btn.cget('text') == '÷':
  65.             div(e.get())
  66.     if btn.cget('text') in ("C", "DEL"):
  67.         if btn.cget('text') == 'C':
  68.             num = e.get()[:-1]
  69.             e.delete(0,'end')
  70.             e.insert('end',num)
  71.         if btn.cget('text') == 'DEL':
  72.             e.delete(0,'end')
  73.     if btn.cget('text') == '=':
  74.         equals()
  75.     e.focus()
  76.  
  77. def validation(inp):
  78.     if inp.isdigit():
  79.         return True
  80.     elif inp == '':
  81.         return True
  82.     else:
  83.         return False
  84.  
  85. reg = root.register(validation)
  86. e = Entry(root,fg='white',font=('',24,'bold'),bg='#1e2936',validatecommand=(reg,'%P'),validate='key',bd=0,justify='right')
  87. e.grid(row=0,column=0,columnspan=4,sticky='news',pady=10,padx=5)
  88. e.focus()
  89.  
  90. btns = []
  91. img = PhotoImage(width=1,height=1)
  92.  
  93. for i in range(5):
  94.     for j in range(4):
  95.         text = texts[4*i+j]
  96.         if text in '1234567890.': color = "#10B981"
  97.         elif text in ('+', '-', 'x', '÷', '√', 'x²'): color = "#F59E0B"
  98.         elif text in "=": color = "#047857"
  99.         elif text in ("C", "DEL"): color = "#2563EB"
  100.  
  101.         btn = Button(root,text=text,fg='white',image=img,compound='t',width=100)
  102.         btn.config(height=100,relief='flat',font=('',18,'bold'),bg=color)
  103.         btn.config(command=lambda i=btn: evaluator(i))
  104.         btns.append(btn)
  105.         btns[-1].grid(row=i+1,column=j,padx=5,pady=5)
  106.  
  107. root.bind('<plus>',lambda event: add(e.get()))
  108. root.bind('<minus>',lambda event: sub(e.get()))
  109. root.bind('<asterisk>',lambda event: mult(e.get()))
  110. root.bind('<slash>',lambda event: div(e.get()))
  111. # root.bind('<equal>',lambda event: equals())
  112. root.bind('<Return>',lambda event: equals())
  113.  
  114. root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement