-
Notifications
You must be signed in to change notification settings - Fork 0
/
calcmt.py
140 lines (109 loc) · 3.76 KB
/
calcmt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import queue
import threading
import math
import tkinter as tk
from timeit import default_timer as timer
start = timer()
math_functions = {
"sin": math.sin,
"cos": math.cos,
"tan": math.tan,
"log": math.log,
"sqrt": math.sqrt,
"pi": math.pi,
"e": math.e,
}
history = []
def user_input():
while True:
expression = input("Math expression goes here (or 'exit' to quit): ")
if expression.lower() == "exit":
break
result_queue.put(expression)
def calculate():
while True:
expression = result_queue.get()
if expression is None:
break
try:
result = eval(expression, {"__builtins__": None}, math_functions)
print(f"Result: {result}")
add_to_history(expression, result)
except Exception as e:
print(f"Error: {e}")
finally:
result_queue.task_done()
def add_to_history(expression, result):
history.append((expression, result))
def show_history():
print("Calculation History:")
for expr, res in history:
print(f"{expr} = {res}")
# GUI for the calc
def evaluate_expression(event=None):
expression = entry.get()
if mode.get() == "calc":
try:
result = eval(expression, {"__builtins__": None}, math_functions)
result_label.config(text=f"Result: {result}")
add_to_history(expression, result)
except Exception as e:
result_label.config(text=f"Error: {e}")
elif mode.get() == "gas":
try:
gwei = float(expression)
wei = gwei * 1e9
usd = wei * eth_price / 1e18
result_label.config(text=f"{gwei} Gwei = {wei} Wei = ${usd:.6f} USD")
add_to_history(expression, f"{gwei} Gwei = {wei} Wei = ${usd:.6f} USD")
except Exception as e:
result_label.config(text=f"Error: {e}")
def show_history_gui():
history_window = tk.Toplevel(root)
history_window.title("Calculation History")
history_text = tk.Text(history_window, wrap='word')
history_text.pack(expand=True, fill='both')
for expr, res in history:
history_text.insert('end', f"{expr} = {res}\n")
def switch_to_calc_mode():
mode.set("calc")
mode_label.config(text="Mode: Calculator")
def switch_to_gas_mode():
mode.set("gas")
mode_label.config(text="Mode: Gas Converter")
# should be dynamically updated but this serves another purpose
eth_price = 3000
if __name__ == "__main__":
result_queue = queue.Queue()
input_thread = threading.Thread(target=user_input)
calc_thread = threading.Thread(target=calculate)
input_thread.start()
calc_thread.start()
# GUI setup
root = tk.Tk()
root.title("Python Calculator")
mode = tk.StringVar(value="calc")
entry = tk.Entry(root, width=50)
entry.bind("<Return>", evaluate_expression)
entry.pack()
result_label = tk.Label(root, text="Result: ")
result_label.pack()
calculate_button = tk.Button(root, text="Calculate", command=evaluate_expression)
calculate_button.pack()
history_button = tk.Button(root, text="Show History", command=show_history_gui)
history_button.pack()
mode_label = tk.Label(root, text="Mode: Calculator")
mode_label.pack()
calc_mode_button = tk.Button(root, text="Switch to Calculator Mode", command=switch_to_calc_mode)
calc_mode_button.pack()
gas_mode_button = tk.Button(root, text="Switch to Gas Mode", command=switch_to_gas_mode)
gas_mode_button.pack()
root.mainloop()
input_thread.join()
result_queue.put(None) # Signal calc_thread to exit
calc_thread.join()
stop = timer()
timer_message = f"Time: {stop-start} s"
print(timer_message)
show_history()
# Taombawkry