#!/usr/bin/python3 # The MIT License (MIT) # # Copyright © 2020-2025 pacman64 # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the “Software”), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. # pycalc.pyw # # PYthon CALCulator is a GUI app which live-evaluates the python expression # currently in its single-line input, re-running it as it changes. from json import dumps import math from math import \ acos, acosh, asin, asinh, atan, atan2, atanh, ceil, comb, \ copysign, cos, cosh, degrees, dist, e, erf, erfc, exp, expm1, \ fabs, factorial, floor, fmod, frexp, fsum, gamma, gcd, hypot, inf, \ isclose, isfinite, isinf, isnan, isqrt, lcm, ldexp, lgamma, log, \ log10, log1p, log2, modf, nan, nextafter, perm, pi, pow, prod, \ radians, remainder, sin, sinh, sqrt, tan, tanh, tau, trunc, ulp try: from math import cbrt, exp2 except Exception: pass from random import \ betavariate, choice, choices, expovariate, gammavariate, gauss, \ getrandbits, getstate, lognormvariate, normalvariate, paretovariate, \ randbytes, randint, random, randrange, sample, seed, setstate, \ shuffle, triangular, uniform, vonmisesvariate, weibullvariate from statistics import \ bisect_left, bisect_right, fmean, \ geometric_mean, harmonic_mean, mean, median, \ median_grouped, median_high, median_low, mode, multimode, pstdev, \ pvariance, quantiles, stdev, variance try: from statistics import \ correlation, covariance, linear_regression, mul, reduce except Exception: pass from tkinter import Tk, Entry, Label, RIGHT, LEFT, EventType from typing import Callable, Iterable, Any # some convenience aliases to various funcs from the python stdlib geomean = geometric_mean harmean = harmonic_mean sd = stdev popsd = pstdev var = variance popvar = pvariance randbeta = betavariate randexp = expovariate randgamma = gammavariate randlognorm = lognormvariate randnorm = normalvariate randweibull = weibullvariate # some occasionally-useful values kb = 1024 mb = 1024 * kb gb = 1024 * mb tb = 1024 * gb pb = 1024 * tb mole = 602214076000000000000000 mol = mole def check_shortcuts(event: EventType) -> None: char = event.char if char == '': return # quit when esc key is pressed if ord(char) == 27: win.quit() def update_result(event: EventType) -> None: try: expr = input.get() if expr == '': return # square brackets are easier to type and are valid math expr = expr.replace('[', '(').replace(']', ')') res = eval(expr) if isinstance(res, int) or isinstance(res, float): output['text'] = '= {:,}'.format(res) else: output['text'] = res except Exception as e: output['text'] = f'= {e}' win = Tk() win.title('Calculate') input = Entry(font=20, width=35) input.pack(side=LEFT, padx=5) input.bind('', check_shortcuts) input.bind('', update_result) input.focus() output = Label(text='esc quits', font=20) output.pack(side=RIGHT, padx=10, pady=5) win.mainloop()