Python基于PyQt的系统登录界面案例集

提示:本文章先分享一下基于PyQt快速搭建系统登录界面的案例集



提示:以下详细分享一下如何基于PyQt快速搭建系统登录界面的方法

一、使用designer布局界面

使用Qt 设计师(designer.exe)软件初始设置界面的主要控件位置及整体布局,并为每个控件创新唯一的名称,如下图所示。

提示:详情请参考代码中的ui_demo.ui文件.


二、将ui文件转换成py文件

设计好ui文件后,需要将ui格式文件转换成py格式,才能在py代码中应用,调用python的pyuic5工具可以实现,相关代码如下所示。

import os
import os.path

# UI文件所在的路径
dir = '.'

# 列出目录下的所有ui文件
def listUiFile():
    list = []
    files = os.listdir(dir)
    for filename in files:
        # print( dir + os.sep + f  )
        # print(filename)
        if filename == 'ui_demo1.ui':
            if os.path.splitext(filename)[1] == '.ui':
                list.append(filename)

    return list

# 把后缀为ui的文件改成后缀为py的文件名
def transPyFile(filename):
    return os.path.splitext(filename)[0] + '.py'

# 调用系统命令把ui转换成py
def runMain():
    list = listUiFile()
    for uifile in list:
        pyfile = transPyFile(uifile)
        cmd = 'pyuic5 -o {pyfile} {uifile}'.format(pyfile=pyfile, uifile=uifile)
        # print(cmd)
        os.system(cmd)

三、使用QSS设置界面样式

独立建立qss样式定义文件,用于统一设置界面的样式,样式内容如下所示。

* {
font-family:'微软雅黑';
color:white;
}

/*设置背景图片*/
QMainWindow{
border-image:url("./images/bg.jpg");
}

QToolButton{
border:0px;
cursor: pointer;
font-size:16px;
}

QLineEdit{
border:0px;
font-size:16px;
background-color: transparent;
}

QWidget[objectName="loginWin"]{
border-image:url("./images/logo_bg.png");
}

QWidget[objectName="widgetUser"]{
border-image:url("./images/usr_bg.png");
}

QWidget[objectName="widgetPwd"]{
border-image:url("./images/pwd_bg.png");
}

QToolButton[objectName="okButton"] {
border-style:none;
border:1px solid #242424;
color:#DCDCDC;
padding:5px;
border-radius:5px;
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #00e4ff,stop:1 #01727f);
}

QToolButton[objectName="okButton"]:hover {
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #007785,stop:1 #004e57);
}

QToolButton[objectName="okButton"]:pressed {
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #00e4ff,stop:1 #01727f);
}

QToolButton[objectName="cancelButton"] {
border-style:none;
border:1px solid #242424;
color:#DCDCDC;
padding:5px;
border-radius:5px;
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #a2a1a1,stop:1 #525252);
}

QToolButton[objectName="cancelButton"]:hover {
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #646464,stop:1 #525252);
}

QToolButton[objectName="cancelButton"]:pressed {
background:qlineargradient(spread:pad,x1:0,y1:0,x2:0,y2:1,stop:0 #a2a1a1,stop:1 #525252);
}

四、创建QMainWindow主窗口类

最后,创建主窗口类进行封装显示即可,代码如下图所示。

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
import win32ui
import win32api,win32con

from demo1.ui_demo1 import Ui_frmMain

class MyMainWindow(QMainWindow, Ui_frmMain):
    def __init__(self, parent=None):
        super(MyMainWindow, self).__init__(parent)
        self.setupUi(self)
        self.setFixedSize(1200, 680) #固定大小
        self.okButton.clicked.connect(self.ok_action)  # 点击登录按钮
        self.cancelButton.clicked.connect(self.cancel_action)  # 点击取消按钮
        #self.setWindowFlags(Qt.FramelessWindowHint)  # 无边框
        self.startAnimation()
        self.initQSS()

    def adj_login(self, user, pwd):
        '''
        验证登录是否正确
        '''
        return True

    def ok_action(self):
        '''
        点击确定按钮事件
        '''
        user = self.lineEditUser.text().strip()
        pwd = self.lineEditPwd.text().strip()
        if user == '' or pwd == '':
            win32api.MessageBox(0, "用户名或密码不能为空,请重新输入!", "登录提醒", win32con.MB_ICONWARNING)
            return False

        if self.adj_login(user, pwd) == False:
            win32api.MessageBox(0, "用户名或密码不正确,请重新输入!", "登录提醒", win32con.MB_ICONWARNING)
            return False

        self.close()

    def cancel_action(self):
        '''
        点击"取消"按钮事件
        '''
        self.close()

    def closeWindowAnimation(self):
        '''
        关闭时的动画效果
        '''
        self.animation = QPropertyAnimation(self, b"windowOpacity")
        self.animation.setDuration(1000)
        self.animation.setStartValue(1)
        self.animation.setEndValue(0)
        self.animation.start()
        self.animation.finished.connect(self.close)

    def startAnimation(self):
        '''
        启动时的动画效果
        '''
        self.animation = QPropertyAnimation(self, b"windowOpacity")
        self.animation.setDuration(1000)
        self.animation.setStartValue(0)
        self.animation.setEndValue(1)
        self.animation.start()

    def initQSS(self):
        '''
        初始化样式
        '''
        style_file = QFile("style.css")
        if style_file.open(QFile.ReadOnly | QFile.Text):
            stream = QTextStream(style_file)
            style_sheet = stream.readAll()
            #print(style_sheet)
            self.setStyleSheet(style_sheet)

if __name__=="__main__":
    app = QApplication(sys.argv)
    myWin = MyMainWindow()
    myWin.show()
    sys.exit(app.exec_())

总结

如需要获取本文中所有模板案例代码,请参考基于Python PyQt的系统登录界面案例集代码

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

动立信息服务

您的鼓励将是我创作最大的动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值