Skip to content

Instantly share code, notes, and snippets.

@Avery-Woodlief
Created April 11, 2020 13:40
Show Gist options
  • Save Avery-Woodlief/980603a3cd6f5cc70ab3711702b3a731 to your computer and use it in GitHub Desktop.
Save Avery-Woodlief/980603a3cd6f5cc70ab3711702b3a731 to your computer and use it in GitHub Desktop.
This is a calculator GUI using Python3
from tkinter import *
from bokeh.colors.named import black, grey, whitesmoke
root = Tk()
root.title("Homemade Calculator")
root.iconbitmap("C:/Users/Avery/Desktop/Python Programs/Images/Calc.ico")
e = Entry(root, width=90, borderwidth=5, bg=black, fg=whitesmoke)
e.grid(row=0, column=0, columnspan=9, padx=10, pady=10)
# Button Functions
def button_click(number):
current = e.get()
e.delete(0, END)
e.insert(0, str(current) + str(number))
def button_C():
e.delete(0, END)
def button_equal():
second_number = e.get()
e.delete(0, END)
if math == "addition":
e.insert(0, f_num + float(second_number))
if math == "subtraction":
e.insert(0, f_num - float(second_number))
if math == "multiplication":
e.insert(0, f_num * float(second_number))
if math == "division":
e.insert(0, f_num / float(second_number))
if math == "cubed":
e.insert(0, f_num * f_num * f_num)
if math == "squared":
e.insert(0, f_num * f_num)
if math == "modulus":
e.insert(0, f_num % float(second_number))
if math == "delete":
e.insert(0, f_num - f_num)
if math == "1/x":
e.insert(0, 1 / f_num)
if math == "sqrt()":
e.insert(0, f_num**(1/2.0))
if math == "crt()":
e.insert(0, f_num**(1/3.0))
if math == "()":
e.insert(0, (f_num))
def button_add():
first_number = e.get()
global f_num
global math
math = "addition"
f_num = float(first_number)
e.delete(0, END)
def button_subtract():
first_number = e.get()
global f_num
global math
math = "subtraction"
f_num = float(first_number)
e.delete(0, END)
def button_CE():
first_number = e.get()
global f_num
global math
math = "delete"
f_num = float(first_number)
e.delete(0, 1)
def button_multiply():
first_number = e.get()
global f_num
global math
math = "multiplication"
f_num = float(first_number)
e.delete(0, END)
def button_divide():
first_number = e.get()
global f_num
global math
math = "division"
f_num = float(first_number)
e.delete(0, END)
def button_cubed():
first_number = e.get()
global f_num
global math
math = "cubed"
f_num = float(first_number)
e.delete(0, END)
def button_square():
first_number = e.get()
global f_num
global math
math = "squared"
f_num = float(first_number)
e.delete(0, END)
def button_mod():
first_numer = e.get()
global f_num
global math
math = "modulus"
f_num = float(first_numer)
e.delete(0, END)
def button_sd():
first_number = e.get()
global f_num
global math
math = "1/x"
f_num = float(first_number)
e.delete(0, END)
def button_sr():
first_number = e.get()
global f_num
global math
math = "sqrt()"
f_num = float(first_number)
e.delete(0, END)
def button_cr():
first_number = e.get()
global f_num
global math
math = "crt()"
f_num = float(first_number)
e.delete(0, END)
def button_q():
first_number = e.get()
global f_num
global math
math ="()"
f_num = float(first_number)
e.delete(0, END)
# Define buttons
button_1 = Button(root, text="1", padx=54, pady=20, command=lambda: button_click(1))
button_2 = Button(root, text="2", padx=56, pady=20, command=lambda: button_click(2))
button_3 = Button(root, text="3", padx=48, pady=20, command=lambda: button_click(3))
button_4 = Button(root, text="4", padx=48, pady=20, command=lambda: button_click(4))
button_5 = Button(root, text="5", padx=48, pady=20, command=lambda: button_click(5))
button_6 = Button(root, text="6", padx=48, pady=20, command=lambda: button_click(6))
button_7 = Button(root, text="7", padx=48, pady=20, command=lambda: button_click(7))
button_8 = Button(root, text="8", padx=55, pady=20, command=lambda: button_click(8))
button_9 = Button(root, text="9", padx=56, pady=20, command=lambda: button_click(9))
button_0 = Button(root, text="0", padx=48, pady=20, command=lambda: button_click(0))
button_dot = Button(root, text=".", padx=47, pady=20, command=lambda: button_click("."))
button_n = Button(root, text="-/+", padx=32, pady=20, command=lambda: button_click("-"))
button_equal = Button(root, text="=", padx=92, pady=20, command=button_equal)
button_CE = Button(root, text="CE", padx=44, pady=20, command=button_CE)
button_C = Button(root, text="C", padx=46, pady=20, command=button_C)
button_subtract = Button(root, text="-", padx=48, pady=20, command=button_subtract)
button_multiply = Button(root, text="*", padx=48, pady=20, command=button_multiply)
button_divide = Button(root, text="/", padx=39, pady=20, command=button_divide)
button_add = Button(root, text="+", padx=48, pady=20, command=button_add)
button_cubed = Button(root, text="x^3", padx=40, pady=20, command=button_cubed)
button_square = Button(root, text="x^2", padx=40, pady=20, command=button_square)
button_mod = Button(root, text="%", padx=48, pady=20, command=button_mod)
button_sd = Button(root, text="1/x", padx=42, pady=20, command=button_sd)
button_sr = Button(root, text="sqrt( x )", padx=40, pady=20, command=button_sr)
button_cr = Button(root, text="crt( x )", padx=40, pady=20, command=button_cr)
button_q = Button(root, text="( x )", padx=40, pady=20, command=button_q)
# display buttons
button_1.grid(row=2, column=4)
button_2.grid(row=2, column=3)
button_3.grid(row=2, column=2)
button_4.grid(row=2, column=1)
button_5.grid(row=2, column=0)
button_6.grid(row=1, column=6)
button_7.grid(row=1, column=5)
button_8.grid(row=1, column=4)
button_9.grid(row=1, column=3)
button_0.grid(row=1, column=2)
button_dot.grid(row=1, column=7)
button_n.grid(row=1, column=8)
button_q.grid(row=3, column=6)
button_C.grid(row=1, column=1)
button_CE.grid(row=1, column=0)
button_equal.grid(row=3, column=7, columnspan=2)
button_subtract.grid(row=2, column=6)
button_multiply.grid(row=2, column=7)
button_divide.grid(row=2, column=8)
button_add.grid(row=2, column=5)
button_cubed.grid(row=3, column=2)
button_square.grid(row=3, column=1)
button_mod.grid(row=3, column=5)
button_sd.grid(row=3, column=0)
button_sr.grid(row=3, column=3)
button_cr.grid(row=3, column=4)
root.mainloop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment