Skip to content

MVC python example

chat GPT

import sys
from PySide2.QtWidgets import QApplication, QMainWindow, QLabel, QLineEdit, QVBoxLayout, QWidget


class Person:
    def __init__(self, name='', age=0):
        self._name = name
        self._age = age

    def get_name(self):
        return self._name

    def set_name(self, name):
        self._name = name

    def get_age(self):
        return self._age

    def set_age(self, age):
        self._age = age


class PersonController:
    def __init__(self, model):
        self.model = model

    def update_name(self, name):
        self.model.set_name(name)

    def update_age(self, age):
        self.model.set_age(age)


class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

        self.name_label = QLabel("Name:")
        self.name_input = QLineEdit()

        self.age_label = QLabel("Age:")
        self.age_input = QLineEdit()

        layout = QVBoxLayout()
        layout.addWidget(self.name_label)
        layout.addWidget(self.name_input)
        layout.addWidget(self.age_label)
        layout.addWidget(self.age_input)

        widget = QWidget()
        widget.setLayout(layout)
        self.setCentralWidget(widget)


if __name__ == '__main__':
    app = QApplication(sys.argv)

    person_model = Person()
    main_window = MainWindow()
    person_controller = PersonController(person_model)

    main_window.name_input.textChanged.connect(person_controller.update_name)
    main_window.age_input.textChanged.connect(person_controller.update_age)

    main_window.show()

    sys.exit(app.exec_())

In this example, we have a Person class that represents the Model. It encapsulates the data and provides getter and setter methods for accessing and modifying the properties, similar to the previous examples.

The MainWindow class represents the View. It creates the UI elements and sets up the layout, just like before.

The PersonController class acts as the Controller. It receives user input from the View and updates the Model accordingly.

The Controller connects the textChanged signals from the name and age input fields to the corresponding update methods in the Model.

This example follows the MVC pattern by separating the concerns into distinct components. The Controller handles user input, the Model represents the data, and the View handles the UI rendering.

Note that in this example, we have not included any additional logic for displaying or utilizing the data from the Model. The focus is on the separation of responsibilities according to the MVC pattern.

programming patterns