我陷入了某种基本问题。
我有2个python文件,我必须将一个文件的变量调用为另一个文件。但是我无法调用它。
问题已定义
* Login.py
其中有类名Ui_MainWindow_login和名为setupUi_1,retranslateUi_1和signIn的函数
我在setupUi_1函数中定义了self.lineEdit,并在signIn函数中使用.text()从用户那里获取了文本。
获取文本后,我在signIn函数中将文本包含在self.username中
* Otp.py
在Otp.py中,我想在otpValidator函数中调用self.username变量,但我无法调用,请指导。
程序:-
* Login.py
import pyotp
import pyodbc
# from RegistrationForm import *
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMessageBox
db = pyodbc.connect("Driver={SQL SERVER};"
"Server=DESKTOP-CMG9I4Q;"
"UID=sa;"
"PWD=shalimar;"
"DATABASE=master;")
cursor = db.cursor()
class Ui_MainWindow_login(object):
def __init__(self):
super(Ui_MainWindow_login, self).__init__()
def setupUi_1(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(811, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(90, 40, 591, 391))
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.groupBox = QtWidgets.QGroupBox(self.frame)
self.groupBox.setGeometry(QtCore.QRect(50, 130, 501, 211))
self.groupBox.setObjectName("groupBox")
self.label = QtWidgets.QLabel(self.groupBox)
self.label.setGeometry(QtCore.QRect(50, 60, 71, 16))
self.label.setObjectName("label")
self.lineEdit = QtWidgets.QLineEdit(self.groupBox) <---- self.lineEdit defined
self.lineEdit.setGeometry(QtCore.QRect(150, 60, 241, 20))
self.lineEdit.setObjectName("lineEdit")
self.label_2 = QtWidgets.QLabel(self.groupBox)
self.label_2.setGeometry(QtCore.QRect(50, 100, 71, 21))
self.label_2.setObjectName("label_2")
self.lineEdit_2 = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_2.setEnabled(True)
self.lineEdit_2.setGeometry(QtCore.QRect(150, 100, 241, 20))
self.lineEdit_2.setEchoMode(QtWidgets.QLineEdit.Password)
self.lineEdit_2.setObjectName("lineEdit_2")
self.lineEdit_3 = QtWidgets.QLineEdit(self.groupBox)
self.lineEdit_3.setGeometry(QtCore.QRect(150, 60, 241, 20))
self.lineEdit_3.setObjectName("lineEdit_3")
self.pushButton = QtWidgets.QPushButton(self.groupBox)
self.pushButton.setGeometry(QtCore.QRect(150, 140, 241, 31))
self.pushButton.setObjectName("pushButton")
self.pushButton.raise_()
self.label.raise_()
self.lineEdit.raise_()
self.label_2.raise_()
self.lineEdit_2.raise_()
self.label_3 = QtWidgets.QLabel(self.frame)
self.label_3.setGeometry(QtCore.QRect(160, 20, 301, 41))
self.label_3.setObjectName("label_3")
self.label_4 = QtWidgets.QLabel(self.frame)
self.label_4.setGeometry(QtCore.QRect(190, 60, 221, 61))
self.label_4.setObjectName("label_4")
self.pushButton.clicked.connect(self.signin)
self.commandLinkButton = QtWidgets.QCommandLinkButton(self.frame)
self.commandLinkButton.setGeometry(QtCore.QRect(380, 340, 185, 41))
self.commandLinkButton.setObjectName("commandLinkButton")
# self.ui.commandLinkButton.clicked.connect(self.dispmessage)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.toolBar = QtWidgets.QToolBar(MainWindow)
self.toolBar.setObjectName("toolBar")
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
self.retranslateUi_1(MainWindow)
# self.lineEdit.textChanged.connect(self.mailAddress1)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
MainWindow.setTabOrder(self.lineEdit, self.lineEdit_2)
MainWindow.setTabOrder(self.lineEdit_2, self.pushButton)
def retranslateUi_1(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.groupBox.setTitle(_translate("MainWindow", "LOGIN"))
self.label.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:10pt; "
"font-weight:600;\">self.username</span></p></body></html>"))
self.label_2.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:10pt; "
"font-weight:600;\">Password</span></p></body></html>"))
self.pushButton.setText(_translate("MainWindow", "Sign In"))
self.label_3.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:24pt; "
"font-weight:600; font-style:italic; "
"color:#ec0000;\">POLYMERUPDATE</span></p><p><br/></p></body"
"></html>"))
self.label_4.setText(_translate("MainWindow", "<html><head/><body><p><span style=\" font-size:18pt; "
"font-weight:600; font-style:italic; color:#ec0000;\">PRICE "
"FORECAST</span></p></body></html>"))
self.commandLinkButton.setText(_translate("MainWindow", "Registration"))
self.toolBar.setWindowTitle(_translate("MainWindow", "toolBar"))
def signin(self):
self.username = self.lineEdit.text() <---- self.username to get text from user
self.password = self.lineEdit_2.text()
login = "select * from Registration_Form where username = ? and Password = ?"
cursor.execute(login, (self.username, self.password))
if self.username == self.password == "":
error_dailog = QtWidgets.QMessageBox()
error_dailog.setIcon(QMessageBox.Information)
error_dailog.setText("Please enter user username and password")
error_dailog.setStandardButtons(QMessageBox.Ok)
error_dailog.exec()
elif self.username == "":
error_dailog = QtWidgets.QMessageBox()
error_dailog.setIcon(QMessageBox.Information)
error_dailog.setText("Please enter your username ")
error_dailog.setStandardButtons(QMessageBox.Ok)
error_dailog.exec()
elif self.password == "":
error_dailog = QtWidgets.QMessageBox()
error_dailog.setIcon(QMessageBox.Information)
error_dailog.setText("Please enter your password")
error_dailog.setStandardButtons(QMessageBox.Ok)
error_dailog.exec()
elif len(cursor.fetchall()) > 0:
totp = pyotp.TOTP('base32secret3232')
s = totp.now()
print(s)
sql = "update otp set OTP = ? where username = ?"
cursor.execute(sql, (s, self.username))
cursor.commit()
error_dailog = QtWidgets.QMessageBox()
error_dailog.setIcon(QMessageBox.Information)
error_dailog.setText("You have logged in")
error_dailog.setStandardButtons(QMessageBox.Ok)
error_dailog.exec()
else:
error_dailog = QtWidgets.QMessageBox()
error_dailog.setIcon(QMessageBox.Information)
error_dailog.setText("Incorrect fields entered")
error_dailog.setStandardButtons(QMessageBox.Ok)
error_dailog.exec()
return
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
ui = Ui_MainWindow_login()
MainWindow = QtWidgets.QMainWindow()
ui.setupUi_1(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
* Otp.py
from PyQt5 import QtCore, QtWidgets
from PyQt5.QtWidgets import QMessageBox
import Login as l1
class Ui_Form_otp(object):
def setupUi(self, Form):
Form.setObjectName("Form")
Form.resize(293, 159)
self.label = QtWidgets.QLabel(Form)
self.label.setGeometry(QtCore.QRect(60, 20, 181, 21))
self.label.setObjectName("label")
self.lineEdit_otp = QtWidgets.QLineEdit(Form)
self.lineEdit_otp.setGeometry(QtCore.QRect(20, 60, 251, 20))
self.lineEdit_otp.setObjectName("lineEdit")
self.pushButton = QtWidgets.QPushButton(Form)
self.pushButton.setGeometry(QtCore.QRect(40, 100, 75, 23))
self.pushButton.setObjectName("pushButton")
self.pushButton_2 = QtWidgets.QPushButton(Form)
self.pushButton_2.setGeometry(QtCore.QRect(170, 100, 75, 23))
self.pushButton_2.setObjectName("pushButton_2")
self.retranslateUi(Form)
QtCore.QMetaObject.connectSlotsByName(Form)
self.pushButton.clicked.connect(self.otpValidator)
def retranslateUi(self, Form):
_translate = QtCore.QCoreApplication.translate
Form.setWindowTitle(_translate("Form", "OTP Verification"))
self.label.setText(_translate("Form", "<html><head/><body><p><span style=\" font-size:9pt; font-weight:600;\">Please enter your OTP here</span></p></body></html>"))
self.pushButton.setText(_translate("Form", "Ok"))
self.pushButton_2.setText(_translate("Form", "Cancel"))
def otpValidator(self):
try:
self.a = self.lineEdit_otp.text()
# I want to call self.username variable here
sql = ("select OTP from otp where OTP = ? ")
dt = l1.cursor.execute(sql, self.a).fetchone()
row = [x for x in dt]
df = str(row)
dg = df[1:7]
print(dg)
if dg == dg:
error_dailog = QtWidgets.QMessageBox()
error_dailog.setIcon(QMessageBox.Information)
error_dailog.setText("You have logged in")
error_dailog.setStandardButtons(QMessageBox.Ok)
error_dailog.exec()
except Exception as e:
print(e)
error_dailog = QtWidgets.QMessageBox()
error_dailog.setIcon(QMessageBox.Information)
error_dailog.setText("Please wait for 120 Seconds and login again")
error_dailog.setStandardButtons(QMessageBox.Ok)
error_dailog.exec()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Form = QtWidgets.QWidget()
ui = Ui_Form_otp()
ui.setupUi(Form)
Form.show()
sys.exit(app.exec_())