Last active
October 26, 2023 04:25
-
-
Save Ved-programmer/b0c8896186fc18c3c386495c779dded3 to your computer and use it in GitHub Desktop.
Compact Calculator by Ved The Pro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tkinter import * | |
from math import * | |
root, WIDTH, HEIGHT = Tk(), 872, 630 | |
root.geometry(f"{WIDTH}x{HEIGHT}");root.title("Ved The Pro Calculator") | |
calculationStringFrame = Frame(root);calculationStringFrame.place(x = 0, y = 0, width = WIDTH, height = 100) | |
calculationOutput = Label(calculationStringFrame, bg = "black", fg = "yellow", text = "", font = ("", 50));calculationOutput.pack(fill = X) | |
calcButtons = Frame(root);calcButtons.place(x = 0, y = 100) | |
buttonLayout = [["+", "-", "*", "/"], ["1", "2", "3", "4"], ["5", "6", "7", "8"], ["9", "0", "=", "b"], ["c", "factorial", "**", ","], ["(", ")", "lcm", "gcd"]] | |
buttonFuncLayoutString = "" | |
for i in buttonLayout: | |
buttonFuncLayoutString += "[" | |
for j in i:buttonFuncLayoutString += f"lambda :change('{j}')," | |
buttonFuncLayoutString += "]," | |
buttonFuncLayout = eval(buttonFuncLayoutString) | |
def change(x): | |
global calculationOutput | |
if x == "c":calculationOutput["text"] = "" | |
elif x == "=": | |
try:calculationOutput["text"] = str(eval(calculationOutput["text"])) | |
except Exception as e: | |
if type(e) == SyntaxError:calculationOutput["text"] = "leading zeros not allowed" | |
elif x == "b":calculationOutput["text"] = calculationOutput["text"][:-1] | |
else: calculationOutput["text"] += x | |
for row in range(len(buttonLayout)): | |
for col in range(len(buttonLayout[0])): | |
button = Button(calcButtons, bg = "black", fg = "yellow", text = buttonLayout[row][col], command = buttonFuncLayout[row][col], width = 8, font = ("", 33));button.grid(row = row, column = col) | |
root.bind("<Return>", lambda x : change("=")) | |
root.mainloop() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment