.
3ss.cn

python通过PyQt5实现登录界面教程

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

在开始实现登录界面前,先给大家普及一下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()
ifdialog.exec_()==QDialog.Accepted:
the_window = MainWindow()
the_window.show()
sys.exit(app.exec_())
赞(0)
未经允许不得转载:互联学术 » python通过PyQt5实现登录界面教程

评论 抢沙发