Saturday, July 18, 2020

Calculator in Python tkinter (Work-in-progress)

from tkinter import *
from random import randint
from time import sleep
  
win = Tk()
win.geometry("210x290+0+0")  #win.geometry("210x325+0+-40")
win.title("BCRS Calculator")
win.resizable(0, 0)
win.config(bg="lightblue")

s = StringVar()
S, fullString, first, second, result, oper = "", "", 0, 0, 0, ''
refreshResult = 'F'

def ifMinus():
  global fullString
  if fullString == '':
    global result, S
    result = 0
    S = '-'
    fullString = '-'
    lbl0.configure(text=fullString)
    lbl1.configure(text=S)
  else:
    ifBinary('-')                     # Invoke ifBinary(char)           

def ifBinary(char):
  global first, oper, S, fullString
  first = float(S)                    # Storing 1st operand
  #btnPlus.config(state=DISABLED);  btnMinus.config(state=DISABLED);
  #btnInto.config(state=DISABLED);  btnBy.config(state=DISABLED);
  oper = char
  S = ''
  fullString += " " + str(char) + " "
  lbl0.configure(text=fullString)

def ifEnter():
  global first, second, oper, result
  second = float(S)                   # Storing 2nd operand
  btnPlus.config(state=NORMAL);  btnMinus.config(state=NORMAL)
  btnInto.config(state=NORMAL);  btnBy.config(state=NORMAL)

  result = eval(fullString)
  if result != int(result):
    result = round(result, 6)
  else:
    result = int(result)
  
  print(first, second, result)
  lbl0.configure(text=str(fullString))
  lbl1.configure(text=str(result))
  global refreshResult
  refreshResult = 'T'
  setBg()

def Click(char):
  global s, S, result, first, second, fullString, oper, refreshResult
  result = 0
  if refreshResult == 'T':
    S = ''
    result = 0
    fullString = ''
    lbl0.config(text=fullString)
    refreshResult = 'F'
  if str(char) in "1234567890.":
    S += str(char)                    # Storing each digit in 1st/2nd operand
    lbl1.configure(text=S)

  fullString += str(char)
  if char == "=":
    fullString = ""
  lbl0.configure(text=fullString)

def Clear():
  txt1.delete(0, END)

def Add():
  pass

lbl0 = Label(win, width=26, bg="grey", font=("Arial", 10, "bold"), text="", anchor="e", padx=0)#, textvariable=s)
lbl0.place(x=0, y=6)

lbl1 = Label(win, width=11, bg="grey", font=("Arial", 22, "bold"), text="0", anchor="e", padx=4)#, textvariable=s)
lbl1.place(x=0, y=26)

#txt1 = Entry(win, width=13, bg="grey", font=("Arial", 20, "bold"), text="0", justify="r")#, textvariable=s)
#txt1.place(x=6, y=280)

btnBack   = Button(win, text="←",   padx=2,  pady=2,  command=Click,               font=("Arial", 13, "bold"), bg="lightgrey", bd=3);  btnBack.place(x=5, y=72)
btnCE     = Button(win, text="Œ",   padx=3,  pady=2,  command=Clear,               font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btnCE.place(x=46, y=72)
btnPlMin  = Button(win, text="±",   padx=5,  pady=2,  command=Click,               font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btnPlMin.place(x=88, y=72)
btnSq     = Button(win, text="x²",  padx=3,  pady=2,  command=Click,               font=("Arial", 13, "bold"), bg="lightgrey", bd=3); btnSq.place(x=127, y=72)
btnSqrt   = Button(win, text="√",   padx=7,  pady=2,  command=Click,               font=("Arial", 13, "bold"), bg="lightgrey", bd=3);  btnSqrt.place(x=168, y=72)

btn7      = Button(win, text="7",   padx=5,  pady=2,  command=lambda:Click(7),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn7.place(x=5, y=114)
btn8      = Button(win, text="8",   padx=5,  pady=2,  command=lambda:Click(8),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn8.place(x=45, y=114)
btn9      = Button(win, text="9",   padx=5,  pady=2,  command=lambda:Click(9),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn9.place(x=85, y=114)
btnBy     = Button(win, text="÷",   padx=6,  pady=2,  command=lambda:ifBinary('/'),  font=("Arial", 13, "bold"), bg="lightgrey", bd=3);  btnBy.place(x=125, y=114)
btnPerc   = Button(win, text="%",   padx=4,  pady=2,  command=Click,               font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btnPerc.place(x=165, y=114)

btn4      = Button(win, text="4",   padx=5,  pady=2,  command=lambda:Click(4),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn4.place(x=5, y=158)
btn5      = Button(win, text="5",   padx=5,  pady=2,  command=lambda:Click(5),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn5.place(x=45, y=158)
btn6      = Button(win, text="6",   padx=5,  pady=2,  command=lambda:Click(6),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn6.place(x=85, y=158)
btnInto   = Button(win, text="*",   padx=6,  pady=2,  command=lambda:ifBinary('*'),  font=("Arial", 13, "bold"), bg="lightgrey", bd=3);  btnInto.place(x=125, y=158)
reci = '\u00B9\u2044\u0078'
btnReci   = Button(win, text=reci,  padx=2,  pady=2,  command=Click,               font=("Arial", 13, "bold"), bg="lightgrey", bd=3);  btnReci.place(x=165, y=158)

btn1      = Button(win, text="1",   padx=5,  pady=2,  command=lambda:Click(1),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn1.place(x=5, y=200)
btn2      = Button(win, text="2",   padx=5,  pady=2,  command=lambda:Click(2),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn2.place(x=45, y=200)
btn3      = Button(win, text="3",   padx=5,  pady=2,  command=lambda:Click(3),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn3.place(x=85, y=200)
btnMinus  = Button(win, text="–",   padx=5,  pady=2,  command=ifMinus,             font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btnMinus.place(x=125, y=200)
enter = '\u23ce'
btnEnter  = Button(win, text=enter, padx=0,  pady=20, command=ifEnter,             font=("Arial", 14, "bold"), bg="lightgrey", bd=3);  btnEnter.place(x=165, y=200)

btn0      = Button(win, text="0",   padx=25, pady=2,  command=lambda:Click(0),     font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btn0.place(x=5, y=243)
btnPoint  = Button(win, text=".",   padx=7,  pady=2,  command=lambda:Click('.'),   font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btnPoint.place(x=85, y=243)
btnPlus   = Button(win, text="+",   padx=5,  pady=2,  command=lambda:ifBinary('+'),  font=("Arial", 12, "bold"), bg="lightgrey", bd=3);  btnPlus.place(x=125, y=243)

def setBg():
  col3 = "#"
  for col1 in range(randint(0, 1000000), randint(10000000, 16777215), randint(100000, 1000000)):
    col2 = hex(col1)
    if col1 < 16:
      col3 += "00000"
    elif col1 < 256:
      col3 += "0000"
    elif col1 < 4096:
      col3 += "000"
    elif col1 < 65536:
      col3 += "00"
    elif col1 < 1048576:
      col3 = "#0"
    else:
      col3 = "#"
    col3 += col2[2:]
    win.config(bg=col3)
    sleep(0.001)
    win.update_idletasks()
    col3 = "#"
    
win.mainloop()

No comments:

Post a Comment