简体   繁体   English

从其他文件访问变量

[英]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. 我正在尝试将代码整理到py文件中,我的GUI带有4个QLineEdit框,并且当单击btnNewPerson时,我希望oerson.py访问输入的值。 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 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 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? 有机地,程序不是一组文件,而是基于模块,库或文件中定义的类创建的对象的交互,因此您的问题应该是:如何将参数传递给Person类的对象? And in this case it is through methods like the constructor. 在这种情况下,它是通过构造函数之类的方法实现的。

publicationgraph.py 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 person.py

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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