0

I am creating an instance of an object, passing it into another object, and then passing it into yet another object like this:

person = Person(fname, lname)
info = Information(person)
summary = Summary(person)

qt_stack_widget.add_child(info)
qt_stack_widget.add_child(summary)

qt_stack_widget.start()
qt_stack_widget.show()

In the Information object, it will collect information with user input then use the person object's mutators to add information to that object.

In Summary, it will take the person object then collect some more information from it and like before mutate the object further.

But the mutations performed in Information on the person object is not reflected in the person object in Summary.

Does anyone know why this would be?

I know that the changes are not reflected because I am sending the initial empty object person to info and summary, but how can I make the person object in summary update after the changes in information?

The program works like this:

  1. the user launches the program
  2. the user enters information which is then mutated into person
  3. the user clicks next to go to the next page of the applet
  4. the page displays the information entered in the previous page

So basically once the person object is updated in Information, the changes are not reflected in the person object that was sent to Summary, even though it's the same object.

What I'm asking is, how can I get the person object to reflect the changes in Summary that was done in Information?

Main:

#!/usr/bin/python3

from PyQt5.QtWidgets import QApplication
import sys
import root_ui
import Individual

import views.launch
import basic_info


def main():
    app = QApplication(sys.argv)
    root = root_ui.RootUI()

    individual = Individual.Individual()

    splash = views.launch.Splash(root.next)
    info = basic_info.Information(individual, root.next)
    summary = views.more_info.Summary(individual, root.next)

    root.add_child(splash)
    root.add_child(info)
    root.add_child(summary)

    root.start()
    root.show()

    app.processEvents()

    sys.exit(app.exec_())


main()

Information

#!/usr/bin/python3

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import *


class Information(QWidget):
    def __init__(self, individual, rt):
        QWidget.__init__(self)
        self.setLayout(self.build(individual, rt))
        self.setFixedHeight(200)

    def firstname(self):
        fname_box = QHBoxLayout()
        fname_label = QLabel("What's your first name?")
        self.__fname_input = QLineEdit()
        fname_box.addStretch(1)
        fname_box.addWidget(fname_label)
        fname_box.addStretch(2)
        fname_box.addWidget(self.__fname_input)

        return fname_box

    def lastname(self):
        lname_box = QHBoxLayout()
        lname_label = QLabel("What's your last name?")
        self.__lname_input = QLineEdit()
        lname_box.addStretch(1)
        lname_box.addWidget(lname_label)
        lname_box.addStretch(2)
        lname_box.addWidget(self.__lname_input)

        return lname_box

    def build(self, individual, rt):
        note = QLabel("Let's start off with some basic information.")

        submit = QPushButton()
        submit.setText("Next!")
        submit.clicked.connect(lambda: self.next_op(individual, rt))

        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(note)
        vbox.addStretch(2)
        vbox.addLayout(self.firstname())
        vbox.addStretch(3)
        vbox.addLayout(self.lastname())
        vbox.addStretch(4)
        vbox.addWidget(submit)

        return vbox

    def next_op(self, individual, rt):
        if self.__fname_input.text() != "" or self.__lname_input.text() != "":
            individual.firstname = self.__fname_input.text()
            individual.lastname = self.__lname_input.text()
            rt()

Summary:

#!/usr/bin/python3

from PyQt5.QtWidgets import *
from PyQt5.QtCore import Qt
from PyQt5.QtGui import *


class Summary(QWidget):
    def __init__(self, individual, rt):
        QWidget.__init__(self)
        self.setLayout(self.build(individual, rt))
        note = QLabel("This is what you have so far:")
        fname = QLabel("First name = {}".format(individual.firstname))
        lname = QLabel("First name = {}".format(individual.lastname))
        
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(note)
        vbox.addStretch(2)
        vbox.addWidget(fname)
        vbox.addStretch(3)
        vbox.addWidget(lname)

        return vbox

Individual:

#!/usr/bin/python3


class Individual:
    def __init__(self):
        self.__first_name = ""
        self.__last_name = ""

    @property
    def firstname(self):
        return self.__first_name

    @firstname.setter
    def firstname(self, fname):
        self.__first_name = fname

    @property
    def lastname(self):
        return self.__last_name

    @lastname.setter
    def lastname(self, lname):
        self.__last_name = lname

    def __str__(self):
        return "First name: {}, Last name: {}".format(self.firstname, self.lastname)
21
  • If you're actually mutating the object, it should do what you expect. So the only explanation is it's not really mutating the object. Without seeing all the relevant code, there's no way to know what you're doing wrong. Commented Apr 15, 2021 at 0:14
  • @Barmar I've added more detail, can you check again please? Commented Apr 15, 2021 at 0:16
  • 1
    Don't describe what the program is doing, show the actual code. The description is what you expect it to do, but you've obviously made a mistake in the implementation. Commented Apr 15, 2021 at 0:16
  • 1
    Well, there are two separate questions here. One is "how can I get the object to update", and I think that's already happening. The other is "how can I get the UI elements to display the updated info?" Is that your real issue? Commented Apr 15, 2021 at 0:18
  • 1
    As an aside, remove the pointless properties from Individual and just use a regular attribute. The whole point of property is not to write boilerplate getters and setters Commented Apr 15, 2021 at 2:01

1 Answer 1

1

Create the Summary first, and pass it to Information. Then next_op() can notify Summary to update its labels.

class Information(QWidget):
    def __init__(self, individual, rt, summary):
        QWidget.__init__(self)
        self.setLayout(self.build(individual, rt))
        self.setFixedHeight(200)
        self.summary = summary

    ...

    def next_op(self, individual, rt):
        if self.__fname_input.text() != "" or self.__lname_input.text() != "":
            individual.firstname = self.__fname_input.text()
            individual.lastname = self.__lname_input.text()
            self.summary.update_labels(individual)
            rt()
class Summary:
    def __init__(self, individual, rt):
        QWidget.__init__(self)
        self.setLayout(self.build(individual, rt))
        note = QLabel("This is what you have so far:")
        self.fname = QLabel("First name = {}".format(individual.firstname))
        self.lname = QLabel("Last name = {}".format(individual.lastname))
        
        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addWidget(note)
        vbox.addStretch(2)
        vbox.addWidget(self.fname)
        vbox.addStretch(3)
        vbox.addWidget(self.lname)

        return vbox

    def update_labels(self, individual):
        self.fname.setText("First name = {}".format(individual.firstname))
        self.lname.setText("Last name = {}".format(individual.lastname))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.