-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcalculator-tk.py
More file actions
executable file
·134 lines (102 loc) · 4.57 KB
/
calculator-tk.py
File metadata and controls
executable file
·134 lines (102 loc) · 4.57 KB
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
#!/usr/bin/env python3
# /// script
# requires-python = ">=3.12"
# dependencies = []
# ///
# https://docs.astral.sh/uv/guides/scripts/#using-a-shebang-to-create-an-executable-file
"""calculator-tk.py here.
at https://github.com/wilsonmar/python-samples/blob/main/calculator-tk.py
Provides a calculator created using the Tk GUI library for Python.
Before running, in CLI run pip install tkinter
USAGE on Terminal:
brew install python-tk@3.12
chmod +x calculator-tk.py
python -m venv .venv # creates bin, include, lib, pyvenv.cfg
source .venv/bin/activate
ruff check calculator-tk.py # for "All checks passed!"
uv run calculator-tk.py # or python calculator-tk.py
"""
__last_change__ = "26-03-11 v007 + add uv prefix lib & rm r in front of leading quote :calculator-tk.py"
__status__ = "Working on macOS Sequoia 15.6.1."
import tkinter as tk
# Create Window
window = tk.Tk()
window.title("My First Calculator")
window.geometry("450x550")
# Holds the value for the input window
current_text = tk.StringVar(window)
current_text.set("0")
# Will contain properties and logic for calculation only
class Calculator:
"""Calculator class."""
def __init__(self):
self.last_number = 0
self.current_operation = ""
self.current_number = 0
def set_operation(self, operation):
"""Set operator."""
self.current_operation = operation
self.last_number = self.current_number
self.current_number = 0
def clear_all(self, text_box):
"""Clear all."""
self.current_operation = ""
self.last_number = 0
self.current_number = 0
text_box.set("0")
def set_number(self, num, text_box):
"""Set number."""
new_num = str(self.current_number) + str(num)
self.current_number = int(new_num)
text_box.set(self.current_number)
def calculate(self, text_box):
"""Calculate."""
match self.current_operation:
case "+":
self.last_number = self.last_number + self.current_number
case "-":
self.last_number = self.last_number - self.current_number
case _:
text_box.set("Error operation not found!")
return
text_box.set(self.last_number)
# We must instantiate a version of the class above
# in order to have our own usable copy of the class
calc = Calculator()
# Input window and Title
display_frame = tk.Frame(window)
tk.Label(display_frame, text="Python Calculator", font=("Georgia", 25), anchor=tk.CENTER).pack()
output = tk.Entry(display_frame, justify=tk.RIGHT, width=45, font=16, state="disabled", textvariable=current_text)
output.pack(pady=20)
display_frame.pack()
button_frame = tk.Frame(window)
# Function avoid duplicating the style information
def create_button(text, command=None):
"""Create button."""
return tk.Button(button_frame, text=text, height=4, width=10, font=20, command=command)
# begin button grid
create_button("CE", command=lambda: calc.clear_all(current_text)).grid(row=0, column=2)
# create_button("/").grid(row=0, column=3)
# Second Row
create_button(7, command=lambda: calc.set_number(7, current_text)).grid(row=1, column=0)
create_button(8, command=lambda: calc.set_number(8, current_text)).grid(row=1, column=1)
create_button(9, command=lambda: calc.set_number(9, current_text)).grid(row=1, column=2)
# create_button("X").grid(row=1, column=3)
# Third Row
create_button(4, command=lambda: calc.set_number(4, current_text)).grid(row=2, column=0)
create_button(5, command=lambda: calc.set_number(5, current_text)).grid(row=2, column=1)
create_button(6, command=lambda: calc.set_number(6, current_text)).grid(row=2, column=2)
create_button("-", command=lambda: calc.set_operation("-")).grid(row=2, column=3)
# Fourth Row
create_button(1, command=lambda: calc.set_number(1, current_text)).grid(row=3, column=0)
create_button(2, command=lambda: calc.set_number(2, current_text)).grid(row=3, column=1)
create_button(3, command=lambda: calc.set_number(3, current_text)).grid(row=3, column=2)
create_button("+", command=lambda: calc.set_operation("+")).grid(row=3, column=3)
# Fifth Row
# create_button("+/-").grid(row=4, column=0)
create_button(0, command=lambda: calc.set_number(0, current_text)).grid(row=4, column=1)
# create_button(".").grid(row=4, column=2)
create_button("=", command=lambda: calc.calculate(current_text)).grid(row=4, column=3)
button_frame.pack()
# main loop to run tkInter GUI
tk.mainloop()