#!/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. # quac.pyw # # Quick Adder/Calculator (QuAC) does what it says with a GUI. 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, Button, Entry, Frame, Label, END, EventType from tkinter.messagebox import showerror from tkinter.ttk import Treeview # 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 # results is a list of formula-result pairs results = [] # quick guide for the treeview-specific api # https://riptutorial.com/tkinter/example/31880/treeview--basic-example def update() -> None: 'Evaluates the current value/formula, then updates the bottom table.' s = inp.get().strip() if s == '': # ignore empty(ish) inputs return try: v = float(eval(s)) inp.config(bg='white', fg='black') results.append((s, v)) except Exception: inp.config(bg='darkred', fg='white') # clear the output for c in out.get_children(): out.delete(c) total = 0 for e in results: total += float(e[1]) win.title(f'Σ ≈ {total:,.4f} (QuAC)') # update output rows j = 0 # the actual row-insertion index for i, e in enumerate(results): # visually-separate groups of 5 rows by adding an empty one if i > 0 and i % 5 == 0: out.insert('', j, text='') j += 1 val = [e[0], f'{e[1]:,.6f}'] out.insert('', j, text=str(j+1), values=val) j += 1 def check_shortcuts(event: EventType) -> None: char = event.char if char == '': return # copy formula and result to clipboard when enter is pressed if ord(char) == 13: update() # quit when esc key is pressed if ord(char) == 27: win.quit() def clear() -> None: global results results = [] # clear the output for c in out.get_children(): out.delete(c) # auto-focus on formula input inp.select_range(0, END) inp.focus_set() # reset title too win.title('Quick Adder/Calculator (QuAC)') try: win = Tk() win.title('Quick Adder/Calculator (QuAC)') win.bind('', lambda _: win.quit()) # top of window: formula-input and button to clear results top = Frame(win) top.pack() Label(top, text='Number or Formula').grid(row=0, column=0, padx=12) inp = Entry(top, text='', width=28, borderwidth=1) inp.grid(row=0, column=1, padx=0) inp.bind('', check_shortcuts) inp.select_range(0, END) inp.focus_set() cl = Button(top, text='Clear', font=8, command=clear) cl.grid(row=0, column=2, padx=0, sticky='e') cl.bind('', lambda _: clear()) # bottom of window only has the formula-results table bottom = Frame(win) bottom.pack(padx=0) out = Treeview(bottom, column=['formula', 'result'], height=36) out.heading('#0', text='#') out.column('#0', width=40) out.heading('formula', text='formula') out.column('formula', width=200, anchor='e') out.heading('result', text='result') out.column('result', width=240, anchor='e') out.pack() # run gui win.mainloop() except Exception as e: showerror('Error', str(e)) win.quit()