Python Forum
First time python user - Calculator code review - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: General (https://python-forum.io/forum-1.html)
+--- Forum: Code Review (https://python-forum.io/forum-46.html)
+--- Thread: First time python user - Calculator code review (/thread-28506.html)



First time python user - Calculator code review - Steamy - Jul-21-2020

I just started learning python a few days ago and I thought I'd mess around and create a calculator for fun. The code itself works fine but I'd appreciate some constructive criticism on the layout and efficiency of my code. I've tried to included an image of the final product running as an .exe file, but I don't know how to post it here. I also have some dependencies that I used for the icon image. Thanks for the help.

from _ast import Lambda
from tkinter import *
from PIL import ImageTk, Image
import tkinter.font as font

root = Tk()
root.resizable(0, 0)
root.configure(background="#84749c", borderwidth=15, relief="raised")


myFont = font.Font(family="Press Start", size=20, weight="bold")


root.title("Retro Calculator")
root.iconbitmap("Calc_icon.ico")


# Create Methods to do Calculations

def button_click(number):
    current = entryBox.get()
    entryBox.delete(0, END)
    entryBox.insert(0, str(current) + str(number))

def button_dec(string):
    current = entryBox.get()
    entryBox.delete(0, END)
    entryBox.insert(0, str(current) + str(string))


def op():
    try:
        global f_num
        first_number = entryBox.get()
        f_num = float(first_number)
        entryBox.delete(0, END)
    except:
        entryBox.delete(0, END)
        entryBox.insert(0, "Error")


def button_add():
    global math
    math = "addition"
    op()


def button_subtract():
    global math
    math = "subtraction"
    op()


def button_divide():
    global math
    math = "division"
    op()


def button_multiply():
    global math
    math = "multiplication"
    op()


def button_clear():
    entryBox.delete(0, END)


def button_eq():
    current = entryBox.get()
    entryBox.delete(0, END)

    if math == "addition":
        entryBox.insert(0, float(f_num) + float(current))


    elif math == "subtraction":
        entryBox.insert(0, float(f_num) - float(current))

    elif math == "multiplication":
        entryBox.insert(0, float(f_num) * float(current))

    elif math == "division":
        try:
            entryBox.insert(0, float(f_num) / float(current))
        except:
            entryBox.insert(0, "Error")


# Create the entry Box
entryBox = Entry(root, width=10, borderwidth=7, bg="#008850", fg="#00e533", relief="sunken")
entryBox["font"] = myFont
# Place Entry Box
entryBox.grid(row=1, column=0, columnspan=4, padx=10, pady=20, ipady=15)

# Create Buttons
button1 = Button(root, text=("1"), command=lambda: button_click(1), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button1["font"] = myFont
button2 = Button(root, text=("2"), command=lambda: button_click(2), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button2["font"] = myFont
button3 = Button(root, text=("3"), command=lambda: button_click(3), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button3["font"] = myFont

button4 = Button(root, text=("4"), command=lambda: button_click(4), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button4["font"] = myFont
button5 = Button(root, text=("5"), command=lambda: button_click(5), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button5["font"] = myFont
button6 = Button(root, text=("6"), command=lambda: button_click(6), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button6["font"] = myFont

button7 = Button(root, text=("7"), command=lambda: button_click(7), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button7["font"] = myFont
button8 = Button(root, text=("8"), command=lambda: button_click(8), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button8["font"] = myFont
button9 = Button(root, text=("9"), command=lambda: button_click(9), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
button9["font"] = myFont
button0 = Button(root, text=("0"), command=lambda: button_click(0), height=2, width=4, padx=15, bg="#84779d", fg="#11141d", borderwidth=7)
button0["font"] = myFont

buttonDiv = Button(root, text=("/"), command=button_divide, height=2, width=2, bg="#c3c4c8", fg="#11141d", borderwidth=7)
buttonDiv["font"] = myFont
buttonMul = Button(root, text=("*"), command=button_multiply, height=2, width=2, bg="#c3c4c8", fg="#11141d", borderwidth=7)
buttonMul["font"] = myFont
buttonSub = Button(root, text=("-"), command=button_subtract, height=2, width=2, bg="#c3c4c8", fg="#11141d", borderwidth=7)
buttonSub["font"] = myFont

buttonAdd = Button(root, text=("+"), command=button_add, height=5, width=2, bg="#c3c4c8", fg="#11141d", borderwidth=7)
buttonAdd["font"] = myFont

buttonEq = Button(root, text=("="), command=button_eq, height=5, width=2, bg="#ffa400", fg="#172752", borderwidth=7)
buttonEq["font"] = myFont
buttonDec = Button(root, text=("."), command=lambda: button_click("."), height=2, width=2, bg="#84779d", fg="#11141d", borderwidth=7)
buttonDec["font"] = myFont
buttonC = Button(root, text=("C"), command=button_clear, height=2, width=2, bg="#ff78a9", fg="#11141d", borderwidth=7)
buttonC["font"] = myFont

# Place Button on screen
button1.grid(row=5, column=0)
button2.grid(row=5, column=1)
button3.grid(row=5, column=2)

button4.grid(row=4, column=0, pady=10)
button5.grid(row=4, column=1)
button6.grid(row=4, column=2)

button7.grid(row=3, column=0)
button8.grid(row=3, column=1)
button9.grid(row=3, column=2)

button0.grid(row=6, column=0, columnspan=2, pady=10)
buttonDec.grid(row=6, column=2)

buttonAdd.grid(row=3, rowspan=2, column=3, ipady=10)
buttonEq.grid(row=5, rowspan=2, column=3, ipady=7)

buttonC.grid(row=2, column=0, pady=10)
buttonDiv.grid(row=2, column=1, padx=4)
buttonMul.grid(row=2, column=2)
buttonSub.grid(row=2, column=3, padx=4)

root.mainloop()



RE: First time python user - Calculator code review - Larz60+ - Jul-22-2020

please attach bitmaps and anything else needed to run