Untitled

mail@pastecode.io avatar
unknown
python
6 months ago
3.5 kB
5
Indexable
Never
import sys
import json
from PyQt5.QtWidgets import QApplication, QWidget, QLabel, QVBoxLayout, QHBoxLayout, QLineEdit, QPushButton, QTextEdit, QMessageBox

class AddressBookApp(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle('Książka Adresowa')
        self.setGeometry(100, 100, 400, 400)

        self.name_label = QLabel('Imię:', self)
        self.name_input = QLineEdit(self)

        self.surname_label = QLabel('Nazwisko:', self)
        self.surname_input = QLineEdit(self)

        self.phone_label = QLabel('Numer telefonu:', self)
        self.phone_input = QLineEdit(self)

        self.street_label = QLabel('Ulica:', self)
        self.street_input = QLineEdit(self)

        self.city_label = QLabel('Miasto:', self)
        self.city_input = QLineEdit(self)

        self.save_button = QPushButton('Zapisz adres', self)
        self.load_button = QPushButton('Wczytaj adresy', self)

        self.result_text = QTextEdit(self)
        self.result_text.setReadOnly(True)

        self.init_ui()

    def init_ui(self):
        vbox = QVBoxLayout()
        vbox.addWidget(self.name_label)
        vbox.addWidget(self.name_input)
        vbox.addWidget(self.surname_label)
        vbox.addWidget(self.surname_input)
        vbox.addWidget(self.phone_label)
        vbox.addWidget(self.phone_input)
        vbox.addWidget(self.street_label)
        vbox.addWidget(self.street_input)
        vbox.addWidget(self.city_label)
        vbox.addWidget(self.city_input)

        hbox_buttons = QHBoxLayout()
        hbox_buttons.addWidget(self.save_button)
        hbox_buttons.addWidget(self.load_button)

        vbox.addLayout(hbox_buttons)
        vbox.addWidget(self.result_text)

        self.setLayout(vbox)

        self.save_button.clicked.connect(self.save_address)
        self.load_button.clicked.connect(self.load_addresses)

    def save_address(self):
        address = {
            'name': self.name_input.text(),
            'surname': self.surname_input.text(),
            'phone': self.phone_input.text(),
            'street': self.street_input.text(),
            'city': self.city_input.text()
        }
        address_book = self.load_addresses()
        if self.verify_address(address, address_book):
            address_book.append(address)
            with open('address_book.json', 'w') as file:
                json.dump(address_book, file)
            self.result_text.append('Adres zapisany pomyślnie.')
        else:
            QMessageBox.warning(self, 'Błąd', 'Adres dla tego użytkownika już istnieje.')

    def load_addresses(self):
        try:
            with open('address_book.json', 'r') as file:
                address_book = json.load(file)
                self.result_text.clear()
                for address in address_book:
                    self.result_text.append(f'Imię: {address["name"]}, Nazwisko: {address["surname"]}, Telefon: {address["phone"]}, Ulica: {address["street"]}, Miasto: {address["city"]}')
                return address_book
        except FileNotFoundError:
            return []

    def verify_address(self, new_address, address_book):
        for address in address_book:
            if address['name'] == new_address['name'] and address['surname'] == new_address['surname']:
                return False
        return True

if __name__ == '__main__':
    app = QApplication(sys.argv)
    address_book = AddressBookApp()
    address_book.show()
    sys.exit(app.exec_())
Leave a Comment