如何在pyqt5中的两个窗口之间传递参数?
Posted
技术标签:
【中文标题】如何在pyqt5中的两个窗口之间传递参数?【英文标题】:How to pass parameters bw two windows in pyqt5? 【发布时间】:2021-08-01 11:19:42 【问题描述】:我正在开发一个密码管理器 gui 应用程序。使用 Qtdesigner 我创建了一个主窗口,我在其中询问用户的用户名和密码。和一个对话窗口,向用户显示不同应用程序和网站的用户名和密码。我还从我的主窗口调用了该对话框窗口,这样当用户按下登录按钮时,如果他们的登录凭据正确,就会出现对话框窗口。
现在我想要的是,当对话框窗口出现时,单击按钮后,它应该显示存储在具有该用户名的表中的用户的所有数据(例如,如果用户名是 alex,那么一个名为 alex 的表数据库中是否有不同应用程序/网站的用户名和密码)。
希望我的问题可以理解并得到回答。如果有人有兴趣回答,我可以进一步澄清。
我的主窗口文件------>
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
from main2 import Ui_Dialog
class Ui_MainWindow(object):
def openwindow(self):
self.window = QtWidgets.QMainWindow()
self.ui = Ui_Dialog()
self.ui.setupUi(self.window)
self.window.show()
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(553, 400)
MainWindow.setMinimumSize(QtCore.QSize(553, 400))
MainWindow.setMaximumSize(QtCore.QSize(553, 400))
MainWindow.setAutoFillBackground(False)
MainWindow.setStyleSheet("background-color:\"PowderBlue\"")
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.label = QtWidgets.QLabel(self.centralwidget)
self.label.setGeometry(QtCore.QRect(-1, -5, 561, 61))
font = QtGui.QFont()
font.setFamily("Dosis ExtraBold")
font.setPointSize(30)
font.setBold(True)
font.setWeight(75)
self.label.setFont(font)
self.label.setStyleSheet("color: rgb(87, 8, 97);")
self.label.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label.setFrameShadow(QtWidgets.QFrame.Plain)
self.label.setLineWidth(7)
self.label.setObjectName("label")
self.frame = QtWidgets.QFrame(self.centralwidget)
self.frame.setGeometry(QtCore.QRect(28, 120, 511, 261))
self.frame.setStyleSheet("background-color: rgb(176, 224, 230);\n"
"")
self.frame.setFrameShape(QtWidgets.QFrame.StyledPanel)
self.frame.setFrameShadow(QtWidgets.QFrame.Raised)
self.frame.setObjectName("frame")
self.username = QtWidgets.QLineEdit(self.frame)
self.username.setGeometry(QtCore.QRect(224, 0, 261, 41))
font = QtGui.QFont()
font.setFamily("Segoe Print")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.username.setFont(font)
self.username.setStyleSheet("border :2px solid purple ;\n"
"background-color:PowderBlue;\n"
"border-radius:5px;\n"
"\n"
"\n"
"\n"
"")
self.username.setText("")
self.username.setEchoMode(QtWidgets.QLineEdit.Normal)
self.username.setPlaceholderText("master user")
self.username.setClearButtonEnabled(False)
self.username.setObjectName("username")
self.label_2 = QtWidgets.QLabel(self.frame)
self.label_2.setGeometry(QtCore.QRect(442, 7, 41, 31))
self.label_2.setStyleSheet("image: url(:/user.png);\n"
"background-color:PowderBlue")
self.label_2.setText("")
self.label_2.setObjectName("label_2")
self.label_3 = QtWidgets.QLabel(self.frame)
self.label_3.setGeometry(QtCore.QRect(450, 69, 31, 31))
self.label_3.setAutoFillBackground(False)
self.label_3.setStyleSheet("image: url(:/key.png);\n"
"background-color:PowderBlue")
self.label_3.setText("")
self.label_3.setObjectName("label_3")
self.password = QtWidgets.QLineEdit(self.frame)
self.password.setGeometry(QtCore.QRect(225, 65, 261, 41))
font = QtGui.QFont()
font.setFamily("Segoe Print")
font.setPointSize(16)
font.setBold(True)
font.setWeight(75)
font.setStyleStrategy(QtGui.QFont.PreferAntialias)
self.password.setFont(font)
self.password.setPlaceholderText("master password")
self.password.setContextMenuPolicy(QtCore.Qt.DefaultContextMenu)
self.password.setStyleSheet("border :2px solid purple ;\n"
"border-radius:5px;")
self.password.setText("")
self.password.setEchoMode(QtWidgets.QLineEdit.Password)
self.password.setPlaceholderText("")
self.password.setClearButtonEnabled(True)
self.password.setObjectName("password")
self.login_button = QtWidgets.QPushButton(self.frame,clicked=lambda:self.log_in())
self.login_button.setGeometry(QtCore.QRect(280, 130, 41, 31))
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(10)
font.setBold(True)
font.setWeight(75)
self.login_button.setFont(font)
self.login_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.login_button.setAcceptDrops(True)
self.login_button.setAutoFillBackground(False)
self.login_button.setStyleSheet("image: url(:/login.png);")
self.login_button.setText("")
self.login_button.setFlat(True)
self.login_button.setObjectName("login_button")
self.signup_button = QtWidgets.QPushButton(self.frame,clicked=lambda:self.sign_up())
self.signup_button.setGeometry(QtCore.QRect(388, 130, 41, 31))
self.signup_button.setCursor(QtGui.QCursor(QtCore.Qt.PointingHandCursor))
self.signup_button.setMouseTracking(False)
self.signup_button.setAcceptDrops(True)
self.signup_button.setAutoFillBackground(False)
self.signup_button.setStyleSheet("image: url(:/user (2).png);")
self.signup_button.setText("")
self.signup_button.setFlat(True)
self.signup_button.setObjectName("signup_button")
self.label_4 = QtWidgets.QLabel(self.frame)
self.label_4.setGeometry(QtCore.QRect(25, 7, 141, 141))
self.label_4.setStyleSheet("image: url(:/team.png);")
self.label_4.setText("")
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(self.frame)
self.label_5.setGeometry(QtCore.QRect(17, 135, 171, 41))
font = QtGui.QFont()
font.setFamily("Forte")
font.setPointSize(22)
font.setBold(False)
font.setWeight(50)
self.label_5.setFont(font)
self.label_5.setStyleSheet("color: rgb(5, 22, 127);")
self.label_5.setFrameShape(QtWidgets.QFrame.NoFrame)
self.label_5.setFrameShadow(QtWidgets.QFrame.Plain)
self.label_5.setLineWidth(7)
self.label_5.setObjectName("label_5")
self.error_lbl = QtWidgets.QLabel(self.frame)
self.error_lbl.setGeometry(QtCore.QRect(230,165,251,50))
#self.error_lbl.textFormat.RichText()
font = QtGui.QFont()
font.setFamily("Roboto Black")
font.setPointSize(11)
font.setBold(True)
font.setWeight(65)
self.error_lbl.setFont(font)
self.error_lbl.setStyleSheet("color: rgb(255,0,0);")
self.error_lbl.setFrameShape(QtWidgets.QFrame.NoFrame)
self.error_lbl.setFrameShadow(QtWidgets.QFrame.Plain)
self.error_lbl.setLineWidth(1)
self.error_lbl.setObjectName("error_lbl")
self.line_2 = QtWidgets.QFrame(self.frame)
self.line_2.setGeometry(QtCore.QRect(-17, 190, 20, 61))
self.line_2.setFrameShape(QtWidgets.QFrame.VLine)
self.line_2.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_2.setObjectName("line_2")
self.line_5 = QtWidgets.QFrame(self.frame)
self.line_5.setGeometry(QtCore.QRect(520, -20, 20, 221))
self.line_5.setFrameShape(QtWidgets.QFrame.VLine)
self.line_5.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_5.setObjectName("line_5")
self.label_6 = QtWidgets.QLabel(self.frame)
self.label_6.setGeometry(QtCore.QRect(349, 231, 41, 31))
self.label_6.setStyleSheet("image: url(:/copyright.png);")
self.label_6.setText("")
self.label_6.setObjectName("label_6")
self.label_7 = QtWidgets.QLabel(self.frame)
self.label_7.setGeometry(QtCore.QRect(380, 234, 111, 20))
font = QtGui.QFont()
font.setFamily("Comic Sans MS")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_7.setFont(font)
self.label_7.setObjectName("label_7")
self.password.raise_()
self.username.raise_()
self.label_2.raise_()
self.label_3.raise_()
self.login_button.raise_()
self.signup_button.raise_()
self.label_4.raise_()
self.label_5.raise_()
self.error_lbl.raise_()
self.line_2.raise_()
self.line_5.raise_()
self.label_6.raise_()
self.label_7.raise_()
self.info_button = QtWidgets.QPushButton(self.centralwidget)
self.info_button.setGeometry(QtCore.QRect(510, 0, 41, 23))
self.info_button.setStyleSheet("image: url(:/information-button.png);")
self.info_button.setText("")
self.info_button.setFlat(True)
self.info_button.setObjectName("info_button")
self.line_3 = QtWidgets.QFrame(self.centralwidget)
self.line_3.setGeometry(QtCore.QRect(9, 101, 20, 271))
self.line_3.setFrameShape(QtWidgets.QFrame.VLine)
self.line_3.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_3.setObjectName("line_3")
self.line_6 = QtWidgets.QFrame(self.centralwidget)
self.line_6.setGeometry(QtCore.QRect(520, 101, 21, 271))
self.line_6.setFrameShape(QtWidgets.QFrame.VLine)
self.line_6.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_6.setObjectName("line_6")
self.label_8 = QtWidgets.QLabel(self.centralwidget)
self.label_8.setGeometry(QtCore.QRect(334, 0, 81, 61))
self.label_8.setStyleSheet("image: url(:/password.png);")
self.label_8.setText("")
self.label_8.setObjectName("label_8")
self.line_8 = QtWidgets.QFrame(self.centralwidget)
self.line_8.setGeometry(QtCore.QRect(19, 90, 511, 20))
self.line_8.setFrameShape(QtWidgets.QFrame.HLine)
self.line_8.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_8.setObjectName("line_8")
self.line_4 = QtWidgets.QFrame(self.centralwidget)
self.line_4.setGeometry(QtCore.QRect(19, 364, 361, 16))
self.line_4.setFrameShape(QtWidgets.QFrame.HLine)
self.line_4.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_4.setObjectName("line_4")
self.line_7 = QtWidgets.QFrame(self.centralwidget)
self.line_7.setGeometry(QtCore.QRect(513, 364, 16, 16))
self.line_7.setFrameShape(QtWidgets.QFrame.HLine)
self.line_7.setFrameShadow(QtWidgets.QFrame.Sunken)
self.line_7.setObjectName("line_7")
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 553, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def log_in(self):
conn = sqlite3.connect("master.db")
cur = conn.cursor()
user = self.username.text()
password = self.password.text()
print(user)
print(password)
if user=="" or password=="" :
self.error_lbl.setText( "materuser & masterpassword\nrequired !")
else:
cur.execute("SELECT rowid , *FROM master_database WHERE master_users = ?", (user,))
c=cur.fetchone()
conn.commit()
conn.close()
if c==None:
self.error_lbl.setText( f"No data for user\nKindly register for new user !" )
else:
if c[2] == password:
#self.error_lbl.setText("Successfully verified.\nClick to Login")
self.window = QtWidgets.QMainWindow()
self.ui = Ui_Dialog()
self.ui.setupUi(self.window)
self.window.show()
else:
self.error_lbl.setText("Incorrect Password !" )
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
我的对话框窗口文件------->'
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main2.ui'
#
# Created by: PyQt5 UI code generator 5.15.4
#
# WARNING: Any manual changes made to this file will be lost when pyuic5 is
# run again. Do not edit this file unless you know what you are doing.
from PyQt5 import QtCore, QtGui, QtWidgets
import sqlite3
class Ui_Dialog(object):
def loaddata(self):
connection = sqlite3.connect('master.db')
query = "SELECT * FROM a"
c = connection.execute(query)
self.tableWidget.setRowCount(0)
for row_number , row_data in enumerate(c):
self.tableWidget.insertRow(row_number)
for column_number ,data in enumerate(row_data):
self.tableWidget.setItem(row_number,column_number,QtWidgets.QTableWidgetItem(str(data)))
connection.commit()
connection.close()
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(553, 424)
font = QtGui.QFont()
font.setFamily("Consolas")
Dialog.setFont(font)
Dialog.setStyleSheet("background-color: PowderBlue;bn ")
self.label_4 = QtWidgets.QLabel(Dialog)
self.label_4.setGeometry(QtCore.QRect(36, 112, 151, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_4.setFont(font)
self.label_4.setObjectName("label_4")
self.label_5 = QtWidgets.QLabel(Dialog)
self.label_5.setGeometry(QtCore.QRect(225, 112, 111, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_5.setFont(font)
self.label_5.setObjectName("label_5")
self.label_6 = QtWidgets.QLabel(Dialog)
self.label_6.setGeometry(QtCore.QRect(396, 112, 121, 31))
font = QtGui.QFont()
font.setFamily("Raleway Black")
font.setPointSize(14)
font.setBold(True)
font.setWeight(75)
self.label_6.setFont(font)
self.label_6.setObjectName("label_6")
self.tableWidget = QtWidgets.QTableWidget(Dialog)
self.tableWidget.setGeometry(QtCore.QRect(10, 150, 531, 271))
self.tableWidget.setMinimumSize(QtCore.QSize(531, 271))
self.tableWidget.setMaximumSize(QtCore.QSize(531, 271))
font = QtGui.QFont()
font.setFamily("Source Sans Pro SemiBold")
font.setPointSize(12)
font.setBold(True)
font.setWeight(75)
self.tableWidget.setFont(font)
self.tableWidget.setStyleSheet("color: rgb(73, 6, 127);")
self.tableWidget.setFrameShape(QtWidgets.QFrame.Box)
self.tableWidget.setGridStyle(QtCore.Qt.NoPen)
self.tableWidget.setRowCount(10)
self.tableWidget.setColumnCount(3)
self.tableWidget.setObjectName("tableWidget")
self.tableWidget.horizontalHeader().setDefaultSectionSize(177)
self.pushButton = QtWidgets.QPushButton(Dialog,clicked=lambda:self.loaddata())
self.pushButton.setGeometry(QtCore.QRect(200, 30, 75, 23))
self.pushButton.setObjectName("pushButton")
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.label_4.setText(_translate("Dialog", "APP/WEBSITE"))
self.label_5.setText(_translate("Dialog", "USERNAME"))
self.label_6.setText(_translate("Dialog", "PASSWORD"))
self.pushButton.setText(_translate("Dialog", "PushButton"))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
Dialog = QtWidgets.QDialog()
ui = Ui_Dialog()
ui.setupUi(Dialog)
Dialog.show()
sys.exit(app.exec_())
【问题讨论】:
【参考方案1】:我使用QStackedWidget 在页面之间切换。
app = QApplication(sys.argv)
stackedwidget = QtWidgets.QStackedWidget()
page1 = PageOneClass()
page2 = PageTwoClass()
stackedwidget.addWidget(page1)
stackedwidget.addWidget(page2)
stackedwidget.show()
app.exec_()
这将让您在页面之间切换而无需运行另一个文件。现在要从一页到另一页获取值,请使用继承。
class MainWindow(QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
self.user = self.username.text()
self password = self.password.text() #Now the user, password can be accessed using the class name. dont forget to add self.
class DialogWindow(MainWindow):
def __init__(self, MainWindow):
super(DialogWindow, self).__init__()
usernamefrompage1 = MainWindow.user
passwordfrompage1 = MainWindow.password
app = QApplication(sys.argv)
stackedwidget = QtWidgets.QStackedWidget()
mainwindow_instance = MainWindow()
dialogwindow_instance = DialogWindow(mainwindow_instance)
#we are passing the mainwindow instance to the dialog window instance, this will let us use all the methods and attributes of the mainwindow.
stackedwidget.addWidget(mainwindow_instance)
stackedwidget.addWidget(dialogwindow_instance)
stackedwidget.setCurrentWidget(mainwindow_instance)
stackedwidget.show()
app.exec_()
【讨论】:
以上是关于如何在pyqt5中的两个窗口之间传递参数?的主要内容,如果未能解决你的问题,请参考以下文章