DiscreteMathematics/Lab_1/main.py

252 lines
10 KiB
Python
Raw Normal View History

2024-03-09 17:44:57 +02:00
import window_1, window_2, window_3, window_4, window_5
from tkinter import *
from tkinter import messagebox
import random
import initial_calculations, simplified_calculations, z_calculations
import shelve
class Screen_Handler():
def get_universal_set(self):
u_boundaries = [window_1.lowest_entry.get(), window_1.highest_entry.get()]
if (not all([item.strip("-").isdigit() for item in u_boundaries])):
messagebox.showerror("Помилка", "Межі універсальної множини мають бути цілими числами.")
return 0
u_boundaries = list(map(int, u_boundaries))
if (u_boundaries[0] >= u_boundaries[1]):
messagebox.showerror("Помилка", "Нижня межа універсальної множини має бути введена раніше першої.")
else:
self.u_boundaries = u_boundaries
self.set_universal_set()
self.universal_set = set(range(u_boundaries[0], u_boundaries[1]+1))
def get_manual_sets(self):
if (not self.check_universal()):
return 0
set_getters_list = [i.get for i in window_1.manual_entries]
sets_abc = [i() for i in set_getters_list]
sets_abc = [set(i().split()) for i in set_getters_list]
if (not all([all([item.strip("-").isdigit() for item in arr]) for arr in sets_abc])):
messagebox.showerror("Помилка", "Множини мають складатися з чисел або бути пустими.")
return 0
sets_abc = [set(map(int, curr_set)) for curr_set in sets_abc]
if (all(map(self.check_manual_sets, sets_abc))):
list(map(self.set_abc_set, sets_abc, window_1.set_labels))
list(map(self.set_abc_set, sets_abc, window_2.set_labels))
list(map(self.set_abc_set, sets_abc, window_3.set_labels))
list(map(self.set_abc_set, sets_abc, window_4.set_labels))
self.sets_abc = sets_abc
def get_generated_sets(self):
if (not self.check_universal()):
return 0
cardinalities = [item.get() for item in window_1.cardinality_entries]
if (not all([item.isdigit() for item in cardinalities])):
messagebox.showerror("Помилка", "Потужності мають бути числами.")
return 0
cardinalities = list(map(int, cardinalities))
if (max(cardinalities) > len(self.universal_set)):
messagebox.showerror("Помилка", "Потужності не можуть перевищувати кількість елементів в універсальній множині.")
return 0
sets_abc = [set(random.sample(range(min(self.universal_set), max(self.universal_set)+1), i)) for i in cardinalities]
list(map(self.set_abc_set, sets_abc, window_1.set_labels))
list(map(self.set_abc_set, sets_abc, window_2.set_labels))
list(map(self.set_abc_set, sets_abc, window_3.set_labels))
list(map(self.set_abc_set, sets_abc, window_4.set_labels))
self.sets_abc = sets_abc
def check_manual_sets(self, curr_set):
if curr_set:
if (max(curr_set) > max(self.universal_set) or min(curr_set) < min(self.universal_set)):
messagebox.showerror("Помилка", "Вихід за межі універсальної множини.")
return False
else:
return True
else:
return True
def initial_calculation_sets(self):
results = initial_calculations.initial_calculations(self.sets_abc, self.universal_set)
result_labels = window_2.calculation_result_labels
for i in range(len(results)):
results_text = set(map(str, results[i]))
results_text = f"{{ {'; '.join(results_text)} }}"
result_labels[i].configure(text=results_text)
window_2.elements_D_label.configure(text=results_text)
self.initial_D = results[-1]
def simple_calculation_sets(self):
results = simplified_calculations.simplified_calculations(self.sets_abc, self.universal_set)
result_labels = window_3.calculation_result_labels
for i in range(len(results)):
results_text = set(map(str, results[i]))
results_text = f"{{ {'; '.join(results_text)} }}"
result_labels[i].configure(text=results_text)
window_3.elements_D_label.configure(text=results_text)
self.simple_D = results[-1]
def z_calculations(self):
custom_results = z_calculations.z_calculations(self.sets_abc, self.universal_set)
result_labels = window_4.step_results
for i in range(len(custom_results)):
results_text = set(map(str, custom_results[i]))
results_text = f"{{ {'; '.join(results_text)} }}"
result_labels[i].configure(text=results_text)
window_4.elements_Z_label.configure(text = results_text)
self.custom_Z = custom_results[-1]
self.default_Z = z_calculations.z_calculations_in(self.sets_abc, self.universal_set)
def set_universal_set(self):
lower_boundary, upper_boundary = map(str, self.u_boundaries)
universal_text = f"{{ {lower_boundary}; ...; {upper_boundary} }}"
window_1.elements_U_label.configure(text = universal_text)
def set_abc_set(self, curr_set, curr_label):
curr_set = set(map(str, curr_set))
curr_text = f"{{ {'; '.join(curr_set)} }}"
curr_label.configure(text = curr_text)
def check_universal(self):
if (not hasattr(self, "universal_set")):
messagebox.showerror("Помилка", "Універсальна множина не задана.")
return False
else:
return True
def save_initial_D(self):
if (not hasattr(self, "initial_D")):
messagebox.showerror("Помилка", "Множина D не знайдена.")
return 0
with shelve.open("save.db") as sh:
sh["initial_D"] = self.initial_D
def save_simple_D(self):
if (not hasattr(self, "simple_D")):
messagebox.showerror("Помилка", "Множина D не знайдена.")
return 0
with shelve.open("save.db") as sh:
sh["simple_D"] = self.simple_D
def save_Z(self):
if (not (hasattr(self, "custom_Z") or hasattr(self, "default_Z"))):
messagebox.showerror("Помилка", "Множина Z не знайдена.")
return 0
with shelve.open("save.db") as sh:
sh["custom_Z"] = self.custom_Z
sh["default_Z"] = self.default_Z
def read_initial_D(self):
with shelve.open("save.db") as sh:
if ("initial_D" in sh.keys()):
initial_D = set(map(str, sh["initial_D"]))
initial_D = f"{{ {'; '.join(initial_D)} }}"
window_5.initial_D_elements.configure(text=initial_D)
else:
messagebox.showerror("Помилка", "Множина D не збережена.")
def read_simple_D(self):
with shelve.open("save.db") as sh:
if ("simple_D" in sh.keys()):
simple_D = set(map(str, sh["simple_D"]))
simple_D = f"{{ {'; '.join(simple_D)} }}"
window_5.simplified_D_elements.configure(text=simple_D)
else:
messagebox.showerror("Помилка", "Множина D не збережена.")
def read_default_Z(self):
with shelve.open("save.db") as sh:
if ("default_Z" in sh.keys()):
default_Z = set(map(str, sh["default_Z"]))
default_Z = f"{{ {'; '.join(default_Z)} }}"
window_5.python_Z_elements.configure(text=default_Z)
else:
messagebox.showerror("Помилка", "Множина Z не збережена.")
def read_custom_Z(self):
with shelve.open("save.db") as sh:
if ("custom_Z" in sh.keys()):
custom_Z = set(map(str, sh["custom_Z"]))
custom_Z = f"{{ {'; '.join(custom_Z)} }}"
window_5.custom_Z_elements.configure(text=custom_Z)
else:
messagebox.showerror("Помилка", "Множина Z не збережена.")
def compare_D(self):
with shelve.open("save.db") as sh:
if (sh["initial_D"] == sh["simple_D"]):
messagebox.showinfo("Порівняння D", "Множини D збігаються.")
else:
messagebox.showinfo("Порівняння D", "Множини D не збігаються.")
def compare_Z(self):
with shelve.open("save.db") as sh:
if (sh["default_Z"] == sh["custom_Z"]):
messagebox.showinfo("Порівняння Z", "Множини Z збігаються")
else:
messagebox.showinfo("Порівняння Z", "Множини Z не збігаються.")
def clear_saves(self):
sh = shelve.open("save.db", flag="n")
sh.close()
def configure_buttons(self):
window_1.set_U_button.configure(command=self.get_universal_set)
window_1.set_manual_button.configure(command=self.get_manual_sets)
window_1.set_generation_button.configure(command=self.get_generated_sets)
window_2.start_calculations_button.configure(command=self.initial_calculation_sets)
window_2.save_D_button.configure(command=self.save_initial_D)
window_3.start_calculations_button.configure(command=self.simple_calculation_sets)
window_3.save_D_button.configure(command=self.save_simple_D)
window_4.start_calculations_button.configure(command=self.z_calculations)
window_4.save_Z_button.configure(command=self.save_Z)
window_5.clear_save_button.configure(command=self.clear_saves)
window_5.read_initial_D_button.configure(command=self.read_initial_D)
window_5.read_simplified_D_button.configure(command=self.read_simple_D)
window_5.read_default_Z_button.configure(command=self.read_default_Z)
window_5.read_custom_Z_button.configure(command=self.read_custom_Z)
window_5.compare_D_button.configure(command=self.compare_D)
window_5.compare_Z_button.configure(command=self.compare_Z)
screens = Screen_Handler()
screens.configure_buttons()
window_1.root.mainloop()