Home > Enterprise >  How do I save what is stored in one variable from one class to another variable in another class?
How do I save what is stored in one variable from one class to another variable in another class?

Time:09-07

As the title says, I am trying to create a variable that points to a variable from another class. I want the summonerName variable, defined in the LoginScreen class and loginfunction function, to be available in the DataPage class. I've tried the below, but I'm getting a missing self argument error. I researched the error and attempted to apply the same concepts (solutions) to my code, but I haven't really had any luck.

class LoginScreen(QDialog):
    def __init__(self):
        super(LoginScreen, self).__init__()
        loadUi("login.ui", self)
        self.passwordfield.setEchoMode(QtWidgets.QLineEdit.Password)
        self.login.clicked.connect(self.loginfunction)
        # self.hyperlink.setText("<a href=\"">Request Access</a>")
        self.hyperlink.setOpenExternalLinks(True)
        self.about.clicked.connect(self.aboutInfo)

    def loginfunction(self):
        user = self.userfield.text()
        password = self.passwordfield.text()

        if len(user) == 0 or len(password) == 0:
            self.error.setText("Please fill out all fields to proceed")
        else:
            db = sqlite3.connect("psudt_login.db")
            # user and password check
            cursor = db.cursor()
            cursor.execute("SELECT * FROM userLogin where username = ? AND password = ?", (user, password))
            row = cursor.fetchone()
            # summoner name grab based on login user
            nextCursor = db.cursor()
            nextCursor.execute("SELECT summonerName FROM userLogin where username = ?", (user,))
            sameRow = nextCursor.fetchone()
            summonerName = sameRow[0]
            if row:
                print("Login successful")
                self.error.setText("")
                dataPage = DataPage()
                widget.addWidget(dataPage)
                widget.setCurrentIndex(widget.currentIndex()   1)
                dataPage.setFixedSize(1600, 1100)
            else:
                self.error.setText("Invalid username or password")
        print("Welcome,", summonerName)
        return summonerName


    def keyPressEvent(self, event): # need to add while loop so that pressing enter on page other than home doesn't re-do login
        if event.key() == Qt.Key_Return:
            LoginScreen.loginfunction(self)

    def aboutInfo(self):
        aboutApp = QMessageBox(self)
        aboutApp.setWindowTitle("About")
        aboutApp.setText("Edited")
        aboutApp.exec()

class DataPage(LoginScreen):
    def __init__(self):
        super(DataPage, self).__init__()
        loadUi("datapage.ui", self)
        summonerName = LoginScreen.loginfunction()

CodePudding user response:

it is useful to define to functions in class DataPage, to get and modify(if needed) it's own variable like:

class DataPage(LoginScreen):
    def __init__(self):
        super(DataPage, self).__init__()
        loadUi("datapage.ui", self)
        self.summonerName = LoginScreen.loginfunction()
    def get_summonerNane(self):
        return self.summonerName

or just using dataPage.summonerName will work too, but the first one is recommoned.

  • Related