简体   繁体   中英

accessing variable from other file

I'm trying to tidy my code into py files, I have the gui with 4 QLineEdit boxes and I want oerson.py to access the inputted vales when the btnNewPerson is clicked. This is summary code which works in a single file, but I am trying to break it up into classes which are manageable.

publicationgraph.py

[other imports ...]
from person import Person
class PublicationGraph(QMainWindow):
    def __init__(self):
        super().__init__()

    self.resize(600, 400)
    self.title = 'title'
    self.left = 10
    self.top = 10
    self.width = 440
    self.height = 280
    self.init_ui()

def init_ui(self):
    self.setWindowTitle(self.title)
    #self.setGeometry(self.left, self.top, self.width, self.height)

    self.lePersonFirstName = QLineEdit('First Name', self)
    self.lePersonFirstName.move(40, 40)

    self.lePersonLastName = QLineEdit('Last Name', self)
    self.lePersonLastName.move(40, 80)

    self.lePersonInitials = QLineEdit('Initials', self)
    self.lePersonInitials.move(40, 120)

    self.btnNewPerson = QPushButton('Add Person', self)
    self.btnNewPerson.move(40, 160)
    self.btnNewPerson.clicked.connect(Person.new)

person.py

class Person():
    def __init__(self):
        super().__init__()

    def new(self):
            name = self.lePersonLastName.text()
            firstName = self.lePersonFirstName.text()
            lastName = self.lePersonLastName.text()
            initials = self.lePersonInitials.text()*

A program organically is not a set of files but is the interaction of objects that are created based on the classes defined in the modules, libraries or files, so your question should be: How to pass the parameters to an object of the class Person? And in this case it is through methods like the constructor.

publicationgraph.py

from PyQt5 import QtCore, QtWidgets
from person import Person

class PublicationGraph(QtWidgets.QMainWindow):
    def __init__(self):
        super().__init__()
        self.init_ui()

        self._persons = []

    def init_ui(self):
        self.lePersonFirstName = QtWidgets.QLineEdit()
        self.lePersonLastName = QtWidgets.QLineEdit()
        self.lePersonInitials = QtWidgets.QLineEdit()
        self.btnNewPerson = QtWidgets.QPushButton('Add Person')
        self.btnNewPerson.clicked.connect(self.on_clicked)

        widget = QtWidgets.QWidget()
        self.setCentralWidget(widget)
        flay = QtWidgets.QFormLayout(widget)
        flay.addRow('First Name', self.lePersonFirstName)
        flay.addRow('Last Name', self.lePersonLastName)
        flay.addRow('Initials', self.lePersonInitials)
        flay.addRow(self.btnNewPerson)

    @QtCore.pyqtSlot()
    def on_clicked(self):
        first_name = self.lePersonFirstName.text()
        last_name = self.lePersonLastName.text()
        initials = self.lePersonInitials.text()

        p = Person(first_name, last_name, initials)
        self._persons.append(p)
        print("Persons: ", self._persons)

if __name__ == '__main__':
    import sys
    app = QtWidgets.QApplication(sys.argv)
    w = PublicationGraph()
    w.show()
    sys.exit(app.exec_())

person.py

class Person():
    def __init__(self, fistname, lastname, initials):
        self._firstname = fistname
        self._lastname = lastname
        self._initials = initials

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM