generated from spseol/PRG-kalkulacka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkalkulacka.py
180 lines (113 loc) · 5.4 KB
/
kalkulacka.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import tkinter as tk
from tkinter import ANCHOR, Frame, messagebox, Listbox, END, ACTIVE
from os.path import basename, splitext
import math
class Application(tk.Tk):
nazev = basename(splitext(basename(__file__.capitalize()))[0])
nazev = "kalkulačkys"
def __init__(self):
super().__init__(className=self.nazev)
self.title(self.nazev)
self.bind("<Escape>", self.quit)
self.protocol("WM_DELETE_WINDOW", self.quit)
self.bind("<Return>", self.insert)
self.var_field = tk.Variable()
self.entry_field = tk.Entry(self, textvariable = self.var_field, width = 40)
self.entry_field.grid(row = 1, column=1, columnspan = 4)
self.listbox = Listbox(self, width = 35)
self.listbox.grid(row = 2, column = 1, pady = 25, columnspan = 4)
self.frame = Frame(self)
self.frame.grid(row = 2, column = 5)
self.btn_up = tk.Button(self.frame, text = "NAHORU", command = self.up, width = 10, border = 5, background = "#c3ffb5")
self.btn_up.pack()
self.btn_down = tk.Button(self.frame, text = "DOLŮ", command = self.down, width = 10, border = 5, background = "#c3ffb5")
self.btn_down.pack()
self.btn_quit = tk.Button(self, text = "ZAVŘÍT", command = self.quit, width = 12, border = 5, background = "#adb1ff")
self.btn_quit.grid(row = 4, column = 2)
self.btn_del = tk.Button(self, text = "SMAZAT", command = self.del_storage, width = 12, border = 5, background = "#adb1ff")
self.btn_del.grid(row = 4, column = 4)
self.storage = []
self.double_operand = {}
self.double_operand["+"] = lambda a, b: a + b
self.double_operand["-"] = lambda a, b: a - b
self.double_operand["*"] = lambda a, b: a * b
self.double_operand["/"] = lambda a, b: a / b
self.double_operand["//"] = lambda a, b: a // b
self.double_operand["**"] = lambda a, b: a ** b
self.single_operand = {}
self.single_operand["sin"] = math.sin
self.single_operand["cos"] = math.cos
self.single_operand["tg"] = math.tan
self.single_operand["tan"] = math.tan
def insert(self, event = None):
raw = self.var_field.get().split()
if len(raw) == 0:
count = 1
else:
count = len(raw)
for i in range(0, count):
if len(raw) == 0:
messagebox.showerror("Chyba", "Nevalidní operace nebo číslo!")
else:
item = raw[i]
if item == "":
messagebox.showerror("Chyba", "Nevalidní operace nebo číslo!")
try:
self.storage.append(float(item))
except:
pass
if item.upper() == "Q":
self.quit()
if item.upper() == "PI":
self.listbox.insert(END, math.pi)
self.storage.append(math.pi)
if item in self.double_operand.keys():
if len(self.storage) >= 2:
b = self.storage.pop()
a = self.storage.pop()
self.storage.append(self.double_operand[item](a, b))
self.listbox.insert(END, self.double_operand[item](a, b))
else:
messagebox.showerror("Chyba", "Nedostatek proměnných")
if item in self.single_operand.keys():
if len(self.storage) >= 1:
a = self.storage.pop()
self.storage.append(self.single_operand[item](a))
self.listbox.insert(END, self.single_operand[item](a))
else:
messagebox.showerror("Chyba", "Nedostatek proměnných")
self.listbox_reload()
def down(self, event = None):
if self.listbox.get(ACTIVE) != "":
item = self.listbox.curselection()[0]
self.storage[item], self.storage[item + 1] = self.storage[item + 1], self.storage[item]
self.listbox_reload()
self.listbox.selection_set(item + 1)
self.listbox.activate(item + 1)
else:
messagebox.showerror("Výběr", "Nic není vybráno vyber nějaké číslo!")
def up(self, event = None):
if self.listbox.get(ACTIVE) != "":
item = self.listbox.curselection()[0]
self.storage[item], self.storage[item - 1] = self.storage[item - 1], self.storage[item]
self.listbox_reload()
self.listbox.selection_set(item - 1)
self.listbox.activate(item - 1)
else:
messagebox.showerror("Výběr", "Nic není vybráno vyber nějaké číslo!")
def del_storage(self):
if self.listbox.get(ANCHOR) != "":
item = self.listbox.curselection()[0]
self.storage.pop(item)
self.listbox_reload()
else:
messagebox.showerror("Výběr", "Nic není vybráno vyber nějaké číslo!")
def listbox_reload(self):
self.var_field.set("")
self.listbox.delete(0, END)
for item in self.storage:
self.listbox.insert(END, item)
def quit(self, event = None):
super().quit()
app = Application()
app.mainloop()