-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalculator.py
76 lines (58 loc) · 2.49 KB
/
calculator.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
"""A simple but kinda useless calculator"""
import tkinter as tk
from functools import partial
from tkinter import ttk
import sv_ttk
import pyAQUATic
class App(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent)
# Make the app responsive
for index in range(4):
self.columnconfigure(index=index, weight=1)
self.rowconfigure(index=index + 1, weight=1)
self.result = tk.StringVar(value="")
# Create widgets
self.setup_widgets()
v.validate('R5', [widget.widgetName for widget in ttk.Frame.winfo_children(self)])
def setup_widgets(self):
self.label = ttk.Label(
self, anchor="e", textvariable=self.result, font=("-size", 15), padding=5
)
self.label.grid(row=0, column=0, columnspan=4, sticky="ew")
for index, key in enumerate("147C2580369=+-*/"):
ttk.Button(self, text=key, style="TButton" if key != "=" else "Accent.TButton",
command=partial(self.button_pressed, key)).grid(row=index % 4 + 1,
column=index // 4,
sticky="nsew",
padx=2, pady=2)
def button_pressed(self, key):
if key == "C":
self.result.set("")
v.validate('R2', self.result.get())
elif key == "=":
self.result.set(str(round(eval(self.result.get()))))
v.validate('R3', self.result.get())
else:
self.result.set(self.result.get() + key)
v.validate('R4', type(self.result.get()).__name__)
def main():
root = tk.Tk()
root.title("Calculator")
root.geometry("300x300")
root.attributes("-topmost", True) # Make it be always-on-top
sv_ttk.set_theme("dark")
v.validate('R1', sv_ttk.get_theme())
app = App(root)
app.pack(fill="both", expand=True)
root.update_idletasks() # Make sure every screen redrawing is done
width, height = root.winfo_width(), root.winfo_height()
x = int((root.winfo_screenwidth() / 2) - (width / 2))
y = int((root.winfo_screenheight() / 2) - (height / 2))
# Set a minsize for the window, and place it in the middle
root.minsize(width, height)
root.geometry(f"+{x}+{y}")
root.mainloop()
if __name__ == "__main__":
v = pyAQUATic.validation('./configs/requirements_001.yaml')
main()