DiscreteMathematics/Lab_2/window_2.py

133 lines
4.2 KiB
Python
Raw Normal View History

2024-03-09 17:44:57 +02:00
from PyQt5 import QtCore, QtWidgets
def save_set(set_to_save, set_name):
import shelve
with shelve.open("dat/saved_sets") as f:
try:
f[set_name] = set_to_save
except:
pass
class Window(QtWidgets.QWidget):
def __init__(self):
super(QtWidgets.QWidget, self).__init__()
self.setWindowTitle("Вікно 2")
self.gridLayout_2 = QtWidgets.QGridLayout(self)
self.A = QtWidgets.QRadioButton(self)
self.A.setText("A")
self.A.setChecked(True)
self.gridLayout_2.addWidget(self.A, 0, 0, 1, 1)
self.B = QtWidgets.QRadioButton(self)
self.B.setText("B")
self.gridLayout_2.addWidget(self.B, 0, 1, 1, 1)
self.label_female = QtWidgets.QLabel(self)
self.label_female.setText("Жінки")
self.label_female.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout_2.addWidget(self.label_female, 1, 0, 1, 1)
self.label_male = QtWidgets.QLabel(self)
self.label_male.setText("Чоловіки")
self.label_male.setAlignment(QtCore.Qt.AlignCenter)
self.gridLayout_2.addWidget(self.label_male, 1, 1, 1, 1)
self.list_female = QtWidgets.QListWidget(self)
self.gridLayout_2.addWidget(self.list_female, 2, 0, 1, 1)
self.list_male = QtWidgets.QListWidget(self)
self.gridLayout_2.addWidget(self.list_male, 2, 1, 1, 1)
self.sets_label = QtWidgets.QLabel(self)
self.sets_label.setText("A: \nB: ")
self.gridLayout_2.addWidget(self.sets_label, 4, 0, 1, 2)
self.clear_A = QtWidgets.QPushButton(self)
self.clear_A.setText("Очистити А")
self.gridLayout_2.addWidget(self.clear_A, 5, 0, 1, 1)
self.clear_B = QtWidgets.QPushButton(self)
self.clear_B.setText("Очистити B")
self.gridLayout_2.addWidget(self.clear_B, 5, 1, 1, 1)
self.save_A = QtWidgets.QPushButton(self)
self.save_A.setText("Зберегти А")
self.gridLayout_2.addWidget(self.save_A, 8, 0, 1, 1)
self.save_B = QtWidgets.QPushButton(self)
self.save_B.setText("Зберегти B")
self.gridLayout_2.addWidget(self.save_B, 8, 1, 1, 1)
self.read_grid = QtWidgets.QGridLayout()
self.read_A = QtWidgets.QPushButton(self)
self.read_A.setText("Читати А")
self.read_grid.addWidget(self.read_A, 1, 0, 1, 1)
self.read_B = QtWidgets.QPushButton(self)
self.read_B.setText("Читати B")
self.read_grid.addWidget(self.read_B, 2, 0, 1, 1)
self.gridLayout_2.addLayout(self.read_grid, 12, 0, 1, 2)
QtCore.QMetaObject.connectSlotsByName(self)
self.set_functions()
self.set_A = set()
self.set_B = set()
def set_functions(self):
self.list_female.itemClicked.connect(self.add_to_set)
self.list_male.itemClicked.connect(self.add_to_set)
self.clear_A.clicked.connect(lambda: self.clear_set(self.set_A))
self.clear_B.clicked.connect(lambda: self.clear_set(self.set_B))
self.save_A.clicked.connect(lambda: save_set(self.set_A, "Set_A"))
self.save_B.clicked.connect(lambda: save_set(self.set_B, "Set_B"))
self.read_A.clicked.connect(lambda: self.read_set(self.set_A, "Set_A"))
self.read_B.clicked.connect(lambda: self.read_set(self.set_B, "Set_B"))
def add_to_set(self, item):
if self.A.isChecked():
self.set_A.add(item.text())
elif self.B.isChecked():
self.set_B.add(item.text())
self.set_set_label_text()
def set_set_label_text(self):
self.sets_label.setText(f"A: {self.set_A}\nB: {self.set_B}".replace("set()", ""))
def set_sets(self, names_f, names_m):
self.list_female.addItems(names_f)
self.list_male.addItems(names_m)
def clear_set(self, set_to_clear):
set_to_clear.clear()
self.set_set_label_text()
def read_set(self, set_to_read, set_name):
import shelve
with shelve.open("dat/saved_sets") as f:
try:
set_to_read.update(f[set_name])
except:
pass
self.set_set_label_text()
def get_set_A(self):
return self.set_A
def get_set_B(self):
return self.set_B