pyside2实现简单的界面跳转
1、前言
废话不说,先上效果图gif:
这里增加简单的判断,如果没有输入名字,则提示,如果没有输入密码,则提示。
当账号和密码都有(任意)的时候,成功跳转到另一个界面。
以此模拟登录界面。
2、代码pyside2
下面看看login.py
代码:
# -*- coding: utf-8 -*-
from PySide2 import QtCore, QtGui, QtWidgets
from PySide2.QtWidgets import *
from login_confirm import *
class Ui_login_form(QtWidgets.QWidget):
def setupUi(self, login_form):
login_form.setObjectName("login_form")
login_form.resize(400, 300)
self.name_label = QtWidgets.QLabel(login_form)
self.name_label.setGeometry(QtCore.QRect(80, 100, 60, 20))
self.name_label.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.name_label.setObjectName("name_label")
self.password_label = QtWidgets.QLabel(login_form)
self.password_label.setGeometry(QtCore.QRect(80, 150, 60, 20))
self.password_label.setAlignment(QtCore.Qt.AlignRight|QtCore.Qt.AlignTrailing|QtCore.Qt.AlignVCenter)
self.password_label.setObjectName("password_label")
self.name_lineedit = QtWidgets.QLineEdit(login_form)
self.name_lineedit.setGeometry(QtCore.QRect(150, 100, 150, 20))
self.name_lineedit.setMaxLength(15)
self.name_lineedit.setEchoMode(QtWidgets.QLineEdit.Normal)
self.name_lineedit.setObjectName("name_lineedit")
self.password_lineedit = QtWidgets.QLineEdit(login_form)
self.password_lineedit.setGeometry(QtCore.QRect(150, 150, 150, 20))
self.password_lineedit.setMaxLength(15)
self.password_lineedit.setEchoMode(QtWidgets.QLineEdit.Password)
self.password_lineedit.setObjectName("password_lineedit")
self.ok_btn = QtWidgets.QPushButton(login_form)
self.ok_btn.setGeometry(QtCore.QRect(220, 200, 80, 20))
self.ok_btn.setObjectName("ok_btn")
self.retranslateUi(login_form)
QtCore.QMetaObject.connectSlotsByName(login_form)
'''
signal&slot
'''
self.ok_btn.clicked.connect(self.login_ok)
def retranslateUi(self, login_form):
_translate = QtCore.QCoreApplication.translate
login_form.setWindowTitle(_translate("login_form", "login"))
self.name_label.setText(_translate("login_form", "name:"))
self.password_label.setText(_translate("login_form", "password:"))
self.ok_btn.setText(_translate("login_form", "Ok"))
def login_ok(self):
if self.name_lineedit.text() == '':
ret = QMessageBox.about(self, 'Tips', 'Please input your name!')
else:
if self.password_lineedit.text() == '':
ret = QMessageBox.about(self, 'Tips', 'Please input your passwords!')
else:
login_info = Ui_login_info_form()
self.window = QWidget()
login_info.setupUi(self.window)
self.window.show()
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
login_form = QtWidgets.QWidget()
ui = Ui_login_form()
ui.setupUi(login_form)
login_form.show()
sys.exit(app.exec_())
login.py
的代码直接由Qt Creator生成.ui
文件通过pyuic5转换而来
转换后,将父类从object
改成QtWidgets.QWidget
即可
其余代码就不摆在这里啦,放在了github仓库里:
Github: https://github.com/cssdcc1997/qt_learning
其他qt系列博客请点击:Qt学习(0) 系列目录
欢迎大家一起讨论~~~~