@ArtemMik
Python

Как передавать Логин из база данных в другие окна PyQt5?

У меня есть приложение PyQt5, в котором есть форма регистрации, после входа/регистрации логин и пароль заносятся в БД, значение логина должно передаваться на другие страницы (например, чтобы к каждого пользователя были свои настройки)

MainWindow:

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
import threading
import pyautogui
from Form import *
import database as db
from database import *
from settings_autoswap import *
from PyQt5.QtCore import pyqtSignal


class Ui_MainWindow(object):
    db_start()
    def setupUi(self, MainWindow):
          #написание страницы

    def add_functions(self):
        self.pushButton_2.clicked.connect(self.start_autoswap)
        self.pushButton_3.clicked.connect(self.stop_autoswap)
        self.pushButton_5.clicked.connect(self.reg)
        self.pushButton_4.clicked.connect(self.settings_autoswap)

    def reg(self):
        self.reg = QtWidgets.QMainWindow()
        self.ui = Ui_Form()
        self.ui.setupUi(self.reg)
        self.reg.show()

    def settings_autoswap(self):
        self.settings_autoswap = QtWidgets.QMainWindow()
        self.ui = Ui_Settings_autoswap()
        self.ui.setupUi(self.settings_autoswap)
        self.settings_autoswap.show()
)


Форма входа/регистрации (из этого файла (Form.py) должно передаваться значение Логина на другие страницы:
from PyQt5 import QtCore, QtGui, QtWidgets
import database as db
from database import *
from PyQt5.QtSql import QSqlDatabase, QSqlQuery, QSqlTableModel, QSqlQueryModel
from PyQt5.QtWidgets import QMessageBox
from autoswap import *
from PyQt5.QtCore import pyqtSignal


class Ui_Form(object):
    value_changed = pyqtSignal(str)
    def setupUi(self, Form):
        Form.setObjectName("Form")
        Form.resize(593, 422)
        Form.setStyleSheet("background-color: rgb(0, 0, 0);")
        self.groupBox = QtWidgets.QGroupBox(Form)
        self.groupBox.setGeometry(QtCore.QRect(130, 40, 321, 321))
        self.groupBox.setStyleSheet("background-color: rgb(42, 43, 56);")
        self.groupBox.setTitle("")
        self.groupBox.setAlignment(QtCore.Qt.AlignCenter)
        self.groupBox.setCheckable(False)
        self.groupBox.setObjectName("groupBox")
        self.label = QtWidgets.QLabel(self.groupBox)
        self.label.setGeometry(QtCore.QRect(80, 10, 151, 61))
        self.label.setStyleSheet("font: 22pt \"MS Shell Dlg 2\";\n"
"border-color: rgb(31, 32, 41);\n"
"color: rgb(255, 255, 255);")
        self.label.setScaledContents(False)
        self.label.setAlignment(QtCore.Qt.AlignCenter)
        self.label.setObjectName("label")
        self.lineEdit = QtWidgets.QLineEdit(self.groupBox)
        self.lineEdit.setGeometry(QtCore.QRect(70, 120, 191, 31))
        self.lineEdit.setStyleSheet("background-color: rgb(31, 32, 41);\n"
"border-color: rgb(31, 32, 41);\n"
"color: rgb(211, 211, 211);")
        self.lineEdit.setText("")
        self.lineEdit.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit.setObjectName("lineEdit")
        self.lineEdit_2 = QtWidgets.QLineEdit(self.groupBox)
        self.lineEdit_2.setGeometry(QtCore.QRect(70, 170, 191, 31))
        self.lineEdit_2.setStyleSheet("background-color: rgb(31, 32, 41);\n"
"\n"
"color: rgb(211, 211, 211);")
        self.lineEdit_2.setText("")
        self.lineEdit_2.setAlignment(QtCore.Qt.AlignCenter)
        self.lineEdit_2.setObjectName("lineEdit_2")
        self.pushButton = QtWidgets.QPushButton(self.groupBox)
        self.pushButton.setGeometry(QtCore.QRect(60, 240, 91, 41))
        self.pushButton.setStyleSheet("background-color: rgb(255, 235, 167);\n"
"font: 14pt \"MS Shell Dlg 2\";\n"
"box-shadow: 0 8px 24px 0 rgb(255 235 167 / 20%);\n"
"color: rgb(94, 102, 129);")
        self.pushButton.setObjectName("pushButton")
        self.pushButton_2 = QtWidgets.QPushButton(self.groupBox)
        self.pushButton_2.setGeometry(QtCore.QRect(180, 240, 91, 41))
        self.pushButton_2.setStyleSheet("background-color: rgb(255, 235, 167);\n"
"font: 14pt \"MS Shell Dlg 2\";\n"
"box-shadow: 0 8px 24px 0 rgb(255 235 167 / 20%);\n"
"color: rgb(94, 102, 129);")
        self.pushButton_2.setObjectName("pushButton_2")
        self.label_2 = QtWidgets.QLabel(self.groupBox)
        self.label_2.setGeometry(QtCore.QRect(20, 120, 47, 21))
        self.label_2.setStyleSheet("font: 14pt \"MS Shell Dlg 2\";\n"
"box-shadow: 0 8px 24px 0 rgb(255 235 167 / 20%);\n"
"color: rgb(94, 102, 129);")
        self.label_2.setAlignment(QtCore.Qt.AlignCenter)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.groupBox)
        self.label_3.setGeometry(QtCore.QRect(10, 170, 61, 21))
        self.label_3.setStyleSheet("font: 10pt \"MS Shell Dlg 2\";\n"
"box-shadow: 0 8px 24px 0 rgb(255 235 167 / 20%);\n"
"color: rgb(94, 102, 129);")
        self.label_3.setAlignment(QtCore.Qt.AlignCenter)
        self.label_3.setObjectName("label_3")

        self.retranslateUi(Form)
        QtCore.QMetaObject.connectSlotsByName(Form)


    def retranslateUi(self, Form):
        _translate = QtCore.QCoreApplication.translate
        Form.setWindowTitle(_translate("Form", "Form"))
        self.label.setText(_translate("Form", "Log IN!"))
        self.pushButton.setText(_translate("Form", "Sign Up"))
        self.pushButton_2.setText(_translate("Form", "Sign In"))
        self.label_2.setText(_translate("Form", "Login"))
        self.label_3.setText(_translate("Form", "Password"))
        self.pushButton_2.clicked.connect(self.reg)
        self.pushButton.clicked.connect(self.login)


    def reg(self):
        login = self.lineEdit.text()
        self.login = login
        password = self.lineEdit_2.text()
        cur.execute('INSERT INTO users (login, password) VALUES (?, ?)',
                    (login, password))
        db.commit()
        msq = QMessageBox()
        msq.setWindowTitle("Регистрация")
        msq.setText("Вы успешно зарегистрировались!")
        x = msq.exec_()
        cur.execute('INSERT INTO settings_autoswap (login, coord_X, coord_y, button) VALUES (?, ?, ?, ?)',
                    (login, 1000, 1000, 2))
        db.commit()
        self.home = QtWidgets.QMainWindow()
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self.home)
        self.home.show()

    def login(self):
        login = self.lineEdit.text()
        password = cur.execute('SELECT password FROM users WHERE login = ?',
                    (login,)).fetchone()
        if password is not None:
            password_input = self.lineEdit_2.text()
            for row in password:
                pas = row
                if pas == password_input:
                    msq = QMessageBox()
                    msq.setWindowTitle("Вход")
                    msq.setText("Вы успешно Вошли!")
                    x = msq.exec_()
                elif pas != password_input:
                    msq = QMessageBox()
                    msq.setWindowTitle("Вход")
                    msq.setText("Неверный пароль, повторите попытку!")
                    x = msq.exec_()
        elif password is None:
            msq = QMessageBox()
            msq.setWindowTitle("Вход")
            msq.setText("Неверный Логин, повторите попытку!")
            x = msq.exec_()
  • Вопрос задан
  • 96 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы