Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from math import log, pow, sqrt, cos, sin, tan, exp
- from tkinter import *
- equation: str = ''
- operation_set = False
- first_element_dot = False
- second_element_dot = False
- def calc(event):
- global equation, operation_set
- if len(equation) != 0:
- txt = field.get()
- operands = txt.split(' ')
- if len(operands) == 3:
- try:
- firstValue = float(operands[0])
- operation = operands[1]
- secondValue = float(operands[2])
- if operation_set:
- if operation == '+':
- equation = str(plus(firstValue, secondValue))
- set_text(equation)
- operation_set = False
- elif operation == '-':
- equation = str(minus(firstValue, secondValue))
- set_text(equation)
- operation_set = False
- elif operation == '×':
- equation = str(multiply(firstValue, secondValue))
- set_text(equation)
- operation_set = False
- elif operation == '÷':
- try:
- equation = str(divide(firstValue, secondValue))
- set_text(equation)
- except ZeroDivisionError:
- equation = 'Error'
- set_text(equation)
- equation = ''
- operation_set = False
- elif operation == '%':
- equation = str(mod(firstValue, secondValue))
- operation_set = False
- val = equation.split('.')
- global first_element_dot, second_element_dot
- second_element_dot = False
- if val[1] == '0':
- equation = val[0]
- first_element_dot = False
- else:
- first_element_dot = True
- set_text(equation)
- except ValueError:
- return
- def pop(event):
- global equation, operation_set, first_element_dot, second_element_dot
- if len(equation) != 0:
- if equation[len(equation) - 1] == ' ':
- equation = equation[:-1]
- equation = equation[:-1]
- equation = equation[:-1]
- operation_set = False
- elif equation[len(equation) - 1] == '.' and not operation_set:
- first_element_dot = False
- equation = equation[:-1]
- elif equation[len(equation) - 1] == '.' and operation_set:
- second_element_dot = False
- equation = equation[:-1]
- else:
- equation = equation[:-1]
- set_text(equation)
- return
- def set_text(text):
- field.configure(state=NORMAL)
- field.delete(0, END)
- field.insert(0, text)
- field.configure(state='readonly')
- return
- def clearAll(event):
- global equation
- equation = ''
- set_text(equation)
- global operation_set, second_element_dot, first_element_dot
- operation_set = False
- second_element_dot = False
- first_element_dot = False
- return
- def pasteOne(event):
- global equation
- equation += str(1)
- set_text(equation)
- return
- def pastePlus(event):
- global operation_set, equation
- if not operation_set and len(equation) != 0:
- operation_set = True
- equation += ' + '
- set_text(equation)
- return
- def pasteMinus(event):
- global operation_set, equation
- if not operation_set and len(equation) != 0:
- operation_set = True
- equation += ' - '
- set_text(equation)
- return
- def pasteMultiplication(event):
- global operation_set, equation
- if not operation_set and len(equation) != 0:
- operation_set = True
- equation += ' × '
- set_text(equation)
- return
- def pasteDivide(event):
- global operation_set, equation
- if not operation_set and len(equation) != 0:
- operation_set = True
- equation += ' ÷ '
- set_text(equation)
- return
- def pasteMod(event):
- global operation_set, equation
- if not operation_set and len(equation) != 0:
- operation_set = True
- equation += ' % '
- set_text(equation)
- return
- def pasteDot(event):
- global equation, operation_set, first_element_dot, second_element_dot
- try:
- if not operation_set and not first_element_dot and equation[len(equation) - 1]:
- equation += '.'
- set_text(equation)
- first_element_dot = True
- elif operation_set and not second_element_dot and equation[len(equation) - 1] != ' ':
- equation += '.'
- set_text(equation)
- second_element_dot = True
- except IndexError:
- return
- def pasteTwo(event):
- global equation
- equation += str(2)
- set_text(equation)
- return
- def pasteA(event):
- global equation
- equation += 'A'
- set_text(equation)
- return
- def pasteB(event):
- global equation
- equation += 'B'
- set_text(equation)
- return
- def pasteC(event):
- global equation
- equation += 'C'
- set_text(equation)
- return
- def pasteD(event):
- global equation
- equation += 'D'
- set_text(equation)
- return
- def pasteE(event):
- global equation
- equation += 'E'
- set_text(equation)
- return
- def pasteF(event):
- global equation
- equation += 'F'
- set_text(equation)
- return
- def pasteThree(event):
- global equation
- equation += str(3)
- set_text(equation)
- return
- # sin, cos, sqrt, tg
- def pasteFour(event):
- global equation
- equation += str(4)
- set_text(equation)
- return
- def pasteFive(event):
- global equation
- equation += str(5)
- set_text(equation)
- return
- def pasteSix(event):
- global equation
- equation += str(6)
- set_text(equation)
- return
- def pasteSeven(event):
- global equation
- equation += str(7)
- set_text(equation)
- return
- def pasteEight(event):
- global equation
- equation += str(8)
- set_text(equation)
- return
- def pasteNine(event):
- global equation
- equation += str(9)
- set_text(equation)
- return
- def pasteZero(event):
- global equation
- equation += str(0)
- set_text(equation)
- return
- def plus(a, b):
- return a + b
- def minus(a, b):
- return a - b
- def divide(a, b):
- return a / b
- def mod(a, b):
- return a % b
- def multiply(a, b):
- return a * b
- def invertValue(event):
- global operation_set, equation
- if not operation_set:
- equation = str(-1 * float(equation))
- val = equation.split('.')
- if val[1] == '0':
- equation = val[0]
- set_text(equation)
- def dec2bin(value):
- if value >= 1:
- g = int(log(value, 2))
- else:
- g = -1
- h = g + 1
- ig = pow(2, g)
- st = ""
- while value > 0 or ig >= 1:
- if value < 1:
- if len(st[h:]) >= 10:
- break
- if value >= ig:
- st += "1"
- value -= ig
- else:
- st += "0"
- ig /= 2
- st = st[:h] + "." + st[h:]
- return st
- # CONVERTING FUNCTIONS
- # DEC
- def bin(event):
- global equation
- if len(equation) == 0:
- return
- if not operation_set:
- try:
- if systemMode == 0:
- equation = dec2bin(float(equation))
- elif systemMode == 1:
- equation = dec2bin(float(format(int(equation, 16))))
- if equation[len(equation) - 1] == '.':
- equation = equation[:-1]
- set_text(equation)
- equation = ''
- except ValueError:
- return
- def hexConversion(event):
- global systemMode, equation
- if len(equation) == 0:
- return
- if not operation_set:
- print(systemMode)
- if systemMode == 0:
- equation = format(int(equation), 'X')
- elif systemMode == 2:
- equation = format(int(int(equation, 2)), 'X')
- set_text(equation)
- def toDec(event):
- global systemMode, equation
- equation = ''
- set_text(equation)
- decBtn.configure(state=DISABLED)
- hexBtn.configure(state=NORMAL)
- binBtn.configure(state=NORMAL)
- modBtn.configure(state=NORMAL)
- plusBtn.configure(state=NORMAL)
- minusBtn.configure(state=NORMAL)
- multiplyBtn.configure(state=NORMAL)
- divideBtn.configure(state=NORMAL)
- cosBtn.configure(state=NORMAL)
- sinBtn.configure(state=NORMAL)
- tgBtn.configure(state=NORMAL)
- sqrtBtn.configure(state=NORMAL)
- sqrtBtn.configure(state=NORMAL)
- dotBtn.configure(state=NORMAL)
- pow2.configure(state=NORMAL)
- pow3.configure(state=NORMAL)
- ex.configure(state=NORMAL)
- invert.configure(state=NORMAL)
- hexA.configure(state=DISABLED)
- hexB.configure(state=DISABLED)
- hexC.configure(state=DISABLED)
- hexD.configure(state=DISABLED)
- hexE.configure(state=DISABLED)
- hexF.configure(state=DISABLED)
- twoBtn.configure(state=NORMAL)
- threeBtn.configure(state=NORMAL)
- fourBtn.configure(state=NORMAL)
- fiveBtn.configure(state=NORMAL)
- sixBtn.configure(state=NORMAL)
- sevenBtn.configure(state=NORMAL)
- eightBtn.configure(state=NORMAL)
- nineBtn.configure(state=NORMAL)
- equalBtn.configure(state=NORMAL)
- systemMode = 0
- def toHex(event):
- global systemMode, equation
- equation = ''
- set_text(equation)
- equalBtn.configure(state=NORMAL)
- hexBtn.configure(state=DISABLED)
- decBtn.configure(state=NORMAL)
- binBtn.configure(state=NORMAL)
- modBtn.configure(state=DISABLED)
- plusBtn.configure(state=DISABLED)
- minusBtn.configure(state=DISABLED)
- pow2.configure(state=DISABLED)
- pow3.configure(state=DISABLED)
- ex.configure(state=DISABLED)
- invert.configure(state=DISABLED)
- multiplyBtn.configure(state=DISABLED)
- divideBtn.configure(state=DISABLED)
- cosBtn.configure(state=DISABLED)
- sinBtn.configure(state=DISABLED)
- tgBtn.configure(state=DISABLED)
- sqrtBtn.configure(state=DISABLED)
- sqrtBtn.configure(state=DISABLED)
- dotBtn.configure(state=DISABLED)
- hexA.configure(state=NORMAL)
- hexB.configure(state=NORMAL)
- hexC.configure(state=NORMAL)
- hexD.configure(state=NORMAL)
- hexE.configure(state=NORMAL)
- hexF.configure(state=NORMAL)
- twoBtn.configure(state=NORMAL)
- threeBtn.configure(state=NORMAL)
- fourBtn.configure(state=NORMAL)
- fiveBtn.configure(state=NORMAL)
- sixBtn.configure(state=NORMAL)
- sevenBtn.configure(state=NORMAL)
- eightBtn.configure(state=NORMAL)
- nineBtn.configure(state=NORMAL)
- equalBtn.configure(state=DISABLED)
- systemMode = 1
- def toBin(event):
- global systemMode, equation
- equation = ''
- set_text(equation)
- equalBtn.configure(state=DISABLED)
- hexBtn.configure(state=NORMAL)
- decBtn.configure(state=NORMAL)
- binBtn.configure(state=DISABLED)
- modBtn.configure(state=DISABLED)
- pow2.configure(state=DISABLED)
- pow3.configure(state=DISABLED)
- ex.configure(state=DISABLED)
- invert.configure(state=DISABLED)
- plusBtn.configure(state=DISABLED)
- minusBtn.configure(state=DISABLED)
- multiplyBtn.configure(state=DISABLED)
- divideBtn.configure(state=DISABLED)
- cosBtn.configure(state=DISABLED)
- sinBtn.configure(state=DISABLED)
- tgBtn.configure(state=DISABLED)
- sqrtBtn.configure(state=DISABLED)
- sqrtBtn.configure(state=DISABLED)
- dotBtn.configure(state=DISABLED)
- hexA.configure(state=DISABLED)
- hexB.configure(state=DISABLED)
- hexC.configure(state=DISABLED)
- hexD.configure(state=DISABLED)
- hexE.configure(state=DISABLED)
- hexF.configure(state=DISABLED)
- twoBtn.configure(state=DISABLED)
- threeBtn.configure(state=DISABLED)
- fourBtn.configure(state=DISABLED)
- fiveBtn.configure(state=DISABLED)
- sixBtn.configure(state=DISABLED)
- sevenBtn.configure(state=DISABLED)
- eightBtn.configure(state=DISABLED)
- nineBtn.configure(state=DISABLED)
- systemMode = 2
- # HEX
- def hex2dec(event):
- global equation
- if len(equation) == 0:
- return
- print(int(equation, 16))
- # BIN
- def getSqrt(event):
- global equation
- if len(equation) == 0:
- return
- if not operation_set:
- equation = str(sqrt(float(equation)))
- val = equation.split('.')
- if val[1] == '0':
- equation = val[0]
- set_text(equation)
- def getCos(event):
- global equation
- if len(equation) == 0:
- return
- if not operation_set:
- equation = str(cos(float(equation)))
- val = equation.split('.')
- if val[1] == '0':
- equation = val[0]
- set_text(equation)
- def decimalConservation(event):
- global systemMode, equation
- if len(equation) == 0:
- return
- if not operation_set:
- print(systemMode)
- if systemMode == 1:
- equation = format(int(equation, 16))
- elif systemMode == 2:
- equation = str(int(equation, 2))
- set_text(equation)
- def getSin(event):
- global equation
- if len(equation) == 0:
- return
- if not operation_set:
- # print(format(int(equation), 'x'))
- equation = str(sin(float(equation)))
- val = equation.split('.')
- if val[1] == '0':
- equation = val[0]
- set_text(equation)
- def getTan(event):
- global equation
- if len(equation) == 0:
- return
- if not operation_set:
- equation = str(tan(float(equation)))
- val = equation.split('.')
- if val[1] == '0':
- equation = val[0]
- set_text(equation)
- def getExp(event):
- global equation
- if len(equation) == 0:
- return
- if not operation_set:
- equation = str(exp(float(equation)))
- val = equation.split('.')
- if val[1] == '0':
- equation = val[0]
- set_text(equation)
- def getPow2(event):
- global equation
- if len(equation) == 0:
- return
- if not operation_set:
- equation = str((float(equation) ** 2))
- val = equation.split('.')
- if val[1] == '0':
- equation = val[0]
- set_text(equation)
- def getPow3(event):
- global equation
- if len(equation) == 0:
- return
- if not operation_set:
- equation = str((float(equation) ** 3))
- val = equation.split('.')
- if val[1] == '0':
- equation = val[0]
- set_text(equation)
- # def hex2dec(event):
- # MAIN CONFIGURATIONS
- root = Tk()
- root.title('Calculator')
- root.resizable(False, False)
- top = Frame(root)
- top.pack(side=TOP)
- center = Frame(root)
- center.pack()
- bottom = Frame(root)
- bottom.pack(side=BOTTOM)
- # FIRST ROW
- field = Entry(top, font=15, width=27)
- field.configure(state='disabled')
- backspace = Button(top, text='<', width=3, fg='magenta', highlightbackground='cyan', highlightthickness=5)
- # SECOND ROW
- clearBtn = Button(center, text='AC', width=6, height=3, font=20)
- modBtn = Button(center, text='mod', width=6, height=3, font=20)
- hexBtn = Button(center, text='HEX', width=6, height=3, font=20)
- binBtn = Button(center, text='BIN', width=6, height=3, font=20)
- decBtn = Button(center, text='DEC', width=6, height=3, font=20, state=DISABLED)
- # THIRD ROW
- hexA = Button(center, text='A', width=6, height=3, font=20, state=DISABLED)
- hexB = Button(center, text='B', width=6, height=3, font=20, state=DISABLED)
- hexC = Button(center, text='C', width=6, height=3, font=20, state=DISABLED)
- pow2 = Button(center, text='x^2', width=6, height=3, font=20)
- pow3 = Button(center, text='x^3', width=6, height=3, font=20)
- # FOURTH ROW
- hexD = Button(center, text='D', width=6, height=3, font=20, state=DISABLED)
- hexE = Button(center, text='E', width=6, height=3, font=20, state=DISABLED)
- hexF = Button(center, text='F', width=6, height=3, font=20, state=DISABLED)
- ex = Button(center, text='e^x', width=6, height=3, font=20)
- invert = Button(center, text='+/-', width=6, height=3, font=20)
- # THIRD ROW
- sevenBtn = Button(center, text='7', width=6, height=3, font=20)
- eightBtn = Button(center, text='8', width=6, height=3, font=20)
- nineBtn = Button(center, text='9', width=6, height=3, font=20)
- multiplyBtn = Button(center, text='×', width=6, height=3, font=20)
- cosBtn = Button(center, text='COS', width=6, height=3, font=20)
- # FOURTH ROW
- fourBtn = Button(center, text='4', width=6, height=3, font=20)
- fiveBtn = Button(center, text='5', width=6, height=3, font=20)
- sixBtn = Button(center, text='6', width=6, height=3, font=20)
- divideBtn = Button(center, text='÷', width=6, height=3, font=20)
- sinBtn = Button(center, text='SIN', width=6, height=3, font=20)
- # FIFTH ROW
- oneBtn = Button(center, text='1', width=6, height=3, font=20)
- twoBtn = Button(center, text='2', width=6, height=3, font=20)
- threeBtn = Button(center, text='3', width=6, height=3, font=20)
- plusBtn = Button(center, text='+', width=6, height=3, font=20)
- tgBtn = Button(center, text='TG', width=6, height=3, font=20)
- # SIXTH ROW
- zeroBtn = Button(center, text='0', width=6, height=3, font=20)
- dotBtn = Button(center, text='•', width=6, height=3, font=20)
- equalBtn = Button(center, text='=', width=6, height=3, font=20)
- minusBtn = Button(center, text='-', width=6, height=3, font=20)
- sqrtBtn = Button(center, text='√', width=6, height=3, font=20)
- # BOTTOM
- systemMode = 0
- # Modes:
- # 0 - decimal
- # 1 - heximal
- # 2 - binary
- decimal = Radiobutton(bottom, text='DEC', variable=systemMode, value=0, state='normal')
- binary = Radiobutton(bottom, text='BIN', variable=systemMode, value=1)
- heximal = Radiobutton(bottom, text='HEX', variable=systemMode, value=2)
- # APPENDING
- field.grid(row=0, column=0, columnspan=2)
- backspace.grid(row=0, column=3)
- clearBtn.grid(row=1, column=0)
- modBtn.grid(row=1, column=1)
- hexBtn.grid(row=1, column=2)
- binBtn.grid(row=1, column=3)
- decBtn.grid(row=1, column=4)
- hexD.grid(row=2, column=0)
- hexE.grid(row=2, column=1)
- hexF.grid(row=2, column=2)
- pow2.grid(row=2, column=3)
- pow3.grid(row=2, column=4)
- hexA.grid(row=3, column=0)
- hexB.grid(row=3, column=1)
- hexC.grid(row=3, column=2)
- ex.grid(row=3, column=3)
- invert.grid(row=3, column=4)
- sevenBtn.grid(row=4, column=0)
- eightBtn.grid(row=4, column=1)
- nineBtn.grid(row=4, column=2)
- multiplyBtn.grid(row=4, column=3)
- cosBtn.grid(row=4, column=4)
- fourBtn.grid(row=5, column=0)
- fiveBtn.grid(row=5, column=1)
- sixBtn.grid(row=5, column=2)
- divideBtn.grid(row=5, column=3)
- sinBtn.grid(row=5, column=4)
- oneBtn.grid(row=6, column=0)
- twoBtn.grid(row=6, column=1)
- threeBtn.grid(row=6, column=2)
- plusBtn.grid(row=6, column=3)
- tgBtn.grid(row=6, column=4)
- zeroBtn.grid(row=7, column=0)
- dotBtn.grid(row=7, column=1)
- equalBtn.grid(row=7, column=2)
- minusBtn.grid(row=7, column=3)
- sqrtBtn.grid(row=7, column=4)
- decimal.grid(row=0, column=0)
- binary.grid(row=0, column=1)
- heximal.grid(row=0, column=2)
- clearBtn.bind('<Button-1>', clearAll)
- oneBtn.bind('<Button-1>', pasteOne)
- twoBtn.bind('<Button-1>', pasteTwo)
- threeBtn.bind('<Button-1>', pasteThree)
- fourBtn.bind('<Button-1>', pasteFour)
- fiveBtn.bind('<Button-1>', pasteFive)
- sixBtn.bind('<Button-1>', pasteSix)
- sevenBtn.bind('<Button-1>', pasteSeven)
- eightBtn.bind('<Button-1>', pasteEight)
- nineBtn.bind('<Button-1>', pasteNine)
- zeroBtn.bind('<Button-1>', pasteZero)
- backspace.bind('<BackSpace>', pop)
- backspace.bind('<Button-1>', pop)
- plusBtn.bind('<Button-1>', pastePlus)
- minusBtn.bind('<Button-1>', pasteMinus)
- modBtn.bind('<Button-1>', pasteMod)
- divideBtn.bind('<Button-1>', pasteDivide)
- multiplyBtn.bind('<Button-1>', pasteMultiplication)
- dotBtn.bind('<Button-1>', pasteDot)
- equalBtn.bind('<Button-1>', calc)
- hexBtn.bind('<Button-1>', hexConversion)
- binBtn.bind('<Button-1>', bin)
- sqrtBtn.bind('<Button-1>', getSqrt)
- tgBtn.bind('<Button-1>', getTan)
- cosBtn.bind('<Button-1>', getCos)
- sinBtn.bind('<Button-1>', getSin)
- decimal.bind('<Button-1>', toDec)
- binary.bind('<Button-1>', toBin)
- heximal.bind('<Button-1>', toHex)
- pow2.bind('<Button-1>', getPow2)
- pow3.bind('<Button-1>', getPow3)
- ex.bind('<Button-1>', getExp)
- decBtn.bind('<Button-1>', decimalConservation)
- hexA.bind('<Button-1>', pasteA)
- hexB.bind('<Button-1>', pasteB)
- hexC.bind('<Button-1>', pasteC)
- hexD.bind('<Button-1>', pasteD)
- hexE.bind('<Button-1>', pasteE)
- hexF.bind('<Button-1>', pasteF)
- invert.bind('<Button-1>', invertValue)
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement