PyQt5 登录界面 python通过PyQt5实现登录界面的代码实例

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

PyQt5 登录界面 python通过PyQt5实现登录界面的代码实例

Python研究者   2021-08-26 我要评论
想了解python通过PyQt5实现登录界面的代码实例的相关内容吗,Python研究者在本文为您仔细讲解PyQt5 登录界面的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:PyQt5,登录界面,PyQt5,登录页面,下面大家一起来学习吧。

今天为大家介绍一个利用开发登录界面模板,基于pyqt5库,pyqt5这也一个PythonGUI界面开发的库,非常强

本例,展示了通过登录界面打开主界面的实现方式。

在开始实现登录界面前,先给大家普及一下PyQt5的安装以及使用

1. pyQt5简单使用

安装

pip install PyQt5
pip3.5 install pyqt5-tools 

界面化操作

1.在win+R中输入designer并敲回车,即可启动Designer。一般选择“Main Window”点击“Create”即可创建。

若在win+R中输入designer并敲回车后无反应,可以直接搜designer.exe直接启动

 2.创建后,可以方便快捷的用Qt Designer画出对应框体,如通过Combo Box添加下拉选择的控件;通过Push Button添加按钮;通过List Widget添加列表框;通过Table Widget添加数据表格框,table中设置列数(右键-Edit Items-Colums),调整框体位置和文字大小,背景颜色以及windowTitle来优化界面显示,使用快捷键Ctrl+R预览当前编写的GUI显示如下:

3.点击保存,生成*.ui的文件,本例中为test.ui,保存在D:\py\deploy文件夹下

2.开始实现登录界面

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
#创建主窗口
class MainWindow(QMainWindow):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.setWindowTitle('主界面')
        self.showMaximized()
#对话框
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_())

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们