python设计登录界面模板_pyqt5 登录界面的实现模板(加强版)

这个教程介绍了如何在Python的PyQt5登录界面基础上增加注销和退出功能。用户可以注销并重新登录,而退出功能则会关闭整个应用程序。代码示例包括了主窗口和登录对话框的详细实现。
摘要由CSDN通过智能技术生成

说明

本例,在登录界面第一版的基础上,增加了主界面的注销功能和退出功能。

注销功能

# 动作一:注销

def on_printAction1_triggered(self):

self.close()

dialog = logindialog()

if dialog.exec_()==QDialog.Accepted:

the_window = MainWindow()

self.windowList.append(the_window) #这句一定要写,不然无法重新登录

the_window.show()

退出功能

# 动作二:退出

def on_printAction2_triggered(self):

self.close()

还是很简单的吧,好了,下面给出完整代码。

完整代码

【如下代码,完全复制,直接运行,即可使用】

import sys

from PyQt5.QtWidgets import *

from PyQt5.QtCore import *

from PyQt5.QtGui import *

################################################

#######创建主窗口

################################################

class MainWindow(QMainWindow):

windowList = []

def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)

self.setWindowTitle('主界面')

self.showMaximized()

# 创建菜单栏

self.createMenus()

def createMenus(self):

# 创建动作 注销

self.printAction1 = QAction(self.tr("注销"), self)

self.printAction1.triggered.connect(self.on_printAction1_triggered)

# 创建动作 退出

self.printAction2 = QAction(self.tr("退出"), self)

self.printAction2.triggered.connect(self.on_printAction2_triggered)

# 创建菜单,添加动作

self.printMenu = self.menuBar().addMenu(self.tr("注销和退出"))

self.printMenu.addAction(self.printAction1)

self.printMenu.addAction(self.printAction2)

# 动作一:注销

def on_printAction1_triggered(self):

self.close()

dialog = logindialog()

if dialog.exec_()==QDialog.Accepted:

the_window = MainWindow()

self.windowList.append(the_window) #这句一定要写,不然无法重新登录

the_window.show()

# 动作二:退出

def on_printAction2_triggered(self):

self.close()

# 关闭界面触发事件

def closeEvent(self, event):

print(999999999)

pass

################################################

#######对话框

################################################

class logindialog(QDialog):

def __init__(self, *args, **kwargs):

super().__init__(*args, **kwargs)

self.setWindowTitle('登录界面')

self.resize(200, 200)

self.setFixedSize(self.width(), self.height())

self.setWindowFlags(Qt.WindowCloseButtonHint)

###### 设置界面控件

self.frame = QFrame(self)

self.verticalLayout = QVBoxLayout(self.frame)

self.lineEdit_account = QLineEdit()

self.lineEdit_account.setPlaceholderText("请输入账号")

self.verticalLayout.addWidget(self.lineEdit_account)

self.lineEdit_password = QLineEdit()

self.lineEdit_password.setPlaceholderText("请输入密码")

self.verticalLayout.addWidget(self.lineEdit_password)

self.pushButton_enter = QPushButton()

self.pushButton_enter.setText("确定")

self.verticalLayout.addWidget(self.pushButton_enter)

self.pushButton_quit = QPushButton()

self.pushButton_quit.setText("取消")

self.verticalLayout.addWidget(self.pushButton_quit)

###### 绑定按钮事件

self.pushButton_enter.clicked.connect(self.on_pushButton_enter_clicked)

self.pushButton_quit.clicked.connect(QCoreApplication.instance().quit)

def on_pushButton_enter_clicked(self):

# 账号判断

if self.lineEdit_account.text() == "":

return

# 密码判断

if self.lineEdit_password.text() == "":

return

# 通过验证,关闭对话框并返回1

self.accept()

################################################

#######程序入门

################################################

if __name__ == "__main__":

app = QApplication(sys.argv)

dialog = logindialog()

if dialog.exec_()==QDialog.Accepted:

the_window = MainWindow()

the_window.show()

sys.exit(app.exec_())

本文如有帮助,敬请留言鼓励。

本文如有错误,敬请留言改进。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值