pyside2/pyqt5两个窗口界面数据传递
本文通过login界面登录成功后弹出main界面,点击发送数据按钮将用用户名传递给main界面。main界面通过返回数据按钮将数据返回到login界面
一、用QT designer创建2个界面login.ui和main.ui
二、代码部分
登录界面代码login.py
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtUiTools import *
from PySide2.QtCore import *
from pyside2_demo.main import *
class Login(QWidget):
signal_1 = Signal(str)
def __init__(self):
super(Login,self).__init__()
self.ui = QUiLoader().load('ui/pyside2_login.ui')
self.ui.loginButton.clicked.connect(self.login_button)
self.ui.sendButton.clicked.connect(self.send_data)
self.main = Main()
self.signal_1.connect(self.main.get_data)
def login_button(self):
user = self.ui.usernameEdit.text()
passwd = self.ui.passwordEdit.text()
if user == 'admin' and passwd =='1234':
self.main.ui.show()
self.main.signal_2.connect(self.get_main_data)
else:
QMessageBox.warning(self.ui,'提示','账号密码错误')
def send_data(self):
self.signal_1.emit(self.ui.usernameEdit.text())
def get_main_data(self,returndata):
self.ui.loginfo.setPlainText(returndata)
if __name__ == '__main__':
app = QApplication([])
stats = Login()
stats.ui.show()
app.exec_()
第2个窗口代码main.py
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtUiTools import *
from PySide2.QtGui import *
class Main(QDialog):
signal_2 = Signal(str)
def __init__(self):
super(Main,self).__init__()
self.ui = QUiLoader().load('ui/main.ui')
self.ui.button_001.clicked.connect(self.return_data)
def return_data(self):
self.signal_2.emit(self.ui.lineEdit_001.text())
def get_data(self,para):
self.ui.lineEdit_001.setText(para)
if __name__ == '__main__':
app = QApplication([])
stats = Main()
stats.ui.show()
app.exec_()
三、最终效果
|