python登录界面demo

版权声明:zhaojanc https://blog.csdn.net/qq_38641985/article/details/82152785
#-*- coding:utf-8 -*-

from  PyQt4.QtGui  import *
from  PyQt4.QtCore  import *
import math
import sys
import re
import os
import json,time
import shutil

reload(sys)
sys.setdefaultencoding("utf-8")

image=QImage()
bgImage=image.load("Ico/loginRe.png")

class Register(QDialog):
    def __init__(self):
        super(Register, self).__init__()
        qss_file = open('white.qss').read()
        self.setStyleSheet(qss_file)
        self.setWindowTitle(u"注册页面")
        palette1 = QPalette(self)
        palette1.setColor(self.backgroundRole(), QColor(192,253,123))   # 设置背景颜色
       # palette1.setBrush(self.backgroundRole(), QtGui.QBrush(QtGui.QPixmap('../../../Document/images/17_big.jpg')))   # 设置背景图片
        self.setPalette(palette1)
        label_user = QLabel(u"账号",self)
        label_user.setGeometry(QRect(75, 55, 50, 22))
        label_passwd = QLabel(u"密码",self)
        label_passwd.setGeometry(QRect(75, 90, 50, 22))
        self.lineEdit_user = QLineEdit(self)
        self.lineEdit_user.setGeometry(QRect(110, 55, 150, 22))

        self.lineEdit_passwd = QLineEdit(self)
        self.lineEdit_passwd.setGeometry(QRect(110, 90, 150, 22))
        self.lineEdit_passwd.setEchoMode(QLineEdit.Password)
        self.lineEdit_passwd.setValidator(QRegExpValidator(QRegExp("[A-Za-z0-9]+"),self))

        SubmitPushbutton=QPushButton(u"提交",self)
        SubmitPushbutton.setGeometry(QRect(110, 135, 50, 25))
        ClosePushbutton =QPushButton(u"关闭",self)
        ClosePushbutton.setGeometry(QRect(190, 135, 50, 25))
        #self.resize(400,300)
        SubmitPushbutton.clicked.connect(self.dataBuild)
        ClosePushbutton.clicked.connect(self.close)
    def dataBuild(self):
        User=str(self.lineEdit_user.text())
        Password=str(self.lineEdit_passwd.text())
        data=[User,Password]
        #print User
        #print Password
        path_json="Register.json"
        list_Json=[]
        if os.path.exists (path_json):
            with open(path_json) as file:
                dict_all = json.loads(file.read())
                list_Json=dict_all
        #print list_Json
        list_Json.append(data)
        with open(path_json,"w") as f:
            json.dump(list_Json,f)
        QMessageBox.information(self, u"提示",u"注册成功")
        self.close()



class labelBtn(QLabel):
    """
    自定义图片按钮类
    """
    def __init__(self,ID):
        super(labelBtn, self).__init__()
        self.setMouseTracking(True)
        self.ID=ID

    def mouseReleaseEvent(self,event):  #注:
        #鼠标点击事件
        self.parent().btnHandle(self.ID)

    def enterEvent(self,event):
        #鼠标进入时间
        self.parent().btnEnter(self.ID)

    def leaveEvent(self,event):
        #鼠标离开事件
        self.parent().btnLeave(self.ID)

class login(QDialog):
    def __init__(self,parent=None):
        super(login, self).__init__(parent)
        self.setWindowTitle(u"项目管理软件")
        #self.setWindowFlags(Qt.Window)

        self.setFixedSize(347,264)
        self.setWindowIcon(QIcon("Ico/software.png"))
        #窗口居中显示
        desktop =QApplication.desktop()
        width = desktop.width()
        height = desktop.height()
        self.move((width - self.width())/2, (height - self.height())/2)
        self.setMouseTracking(True)
        #无边框
        self.setWindowFlags(Qt.FramelessWindowHint)

                                             #右键菜单               

        label_user = QLabel(u"",self)
        label_user.setGeometry(QRect(125, 135, 50, 22))
        label_passwd = QLabel(u"",self)
        label_passwd.setGeometry(QRect(125, 170, 50, 22))



        self.lineEdit_user = QLineEdit(u"",self)
        self.lineEdit_user.setGeometry(QRect(120, 135, 150, 22))

        self.lineEdit_passwd = QLineEdit(u'',self)
        self.lineEdit_passwd.setGeometry(QRect(120, 175, 150, 22))
        self.lineEdit_passwd.setEchoMode(QLineEdit.Password)
        self.lineEdit_passwd.setValidator(QRegExpValidator(QRegExp("[A-Za-z0-9]+"),self))
        #这里也可以设置QLineEdit背景为透明
        self.pushButton_login = QPushButton(QIcon("images/login.png"),u"登录",self)
        self.pushButton_login.setGeometry(QRect(250, 235, 75, 22))

        self.pushButton_change = QPushButton(QIcon("images/onetwo.png"),u"注册",self)
        self.pushButton_change.setGeometry(QRect(10, 235, 75, 22))
        self.pushButton_change.setFlat(True)
        self.pushButton_change.setContextMenuPolicy(Qt.CustomContextMenu)




        self.btn_close=labelBtn(1)              #定义关闭按钮 ID:2
        self.btn_close.setParent(self)
        self.btn_close.setGeometry(310,0,38,21)
        self.btn_close.setToolTip(u"关闭")

        self.connect(self.pushButton_change, SIGNAL("clicked()"),self.contextMenu)
        self.connect(self.pushButton_login, SIGNAL("clicked()"),self.log_in)

    def contextMenu(self):
        self.hide()
        register=Register()
        register.show()
        register.exec_()
        self.showNormal()
        #QMessageBox.information(self, u"提示",u"注册页面未写")

    def log_in(self):
        path_json="Register.json"
        pan_data=0
        if os.path.exists (path_json):
            with open(path_json) as file:
                dict_all = json.loads(file.read())
            for  i in range(len(dict_all)):
                username=dict_all[i][0]
                userpass=dict_all[i][1]
                if self.lineEdit_user.text() == username and self.lineEdit_passwd.text()==userpass:
                    QMessageBox.information(self, u"提示",u"登录成功")
                    pan_data=1
                    self.accept()
        if self.lineEdit_user.text() == 'Admin' and self.lineEdit_passwd.text() == 'admin':   
            # 如果用户名和密码正确,关闭对话框,accept()关闭后,如果增加一个取消按钮调用reject()
            QMessageBox.information(self, u"提示",u"登录成功")
            pan_data=1
            self.accept()

        if pan_data==0:  
            QMessageBox.critical(self, 'Error', 'User name or password error')   


    def btnHandle(self,ID):
        #最小化
        if ID==1:
            self.close()
        else:
           QMessageBox.critical(self, u'提示', u'未知错误')   

    def btnEnter(self,ID):
       #鼠标进入
       if ID == 1:
           self.btn_close.setPixmap(QPixmap("images/close.png"))
       else:
           QMessageBox.critical(self, u'提示', u'未知错误')   

    def btnLeave(self,ID):
       #鼠标离开
       '''false.png这张图片是不存在的,目的是要在鼠标
        离开后还原背景,因为默认按钮我已经PS在背景上了'''
       self.btn_close.setPixmap(QPixmap("images/false.png"))


    def resizeEvent(self,event):

       #重绘窗体背景
       pal=QPalette()
       pal.setBrush(QPalette.Window,QBrush(image.scaled(event.size(),
           Qt.KeepAspectRatioByExpanding,Qt.SmoothTransformation)))

       self.setPalette(pal)

    """下面这两个才是重点,是动得关键"""
    def mousePressEvent(self,event):
       #鼠标点击事件
       if event.button() == Qt.LeftButton:
           self.dragPosition = event.globalPos() - self.frameGeometry().topLeft()
           event.accept()

    def mouseMoveEvent(self,event):

       #鼠标移动事件
        if event.buttons() == Qt.LeftButton:
            self.move(event.globalPos() - self.dragPosition)
            event.accept()



class Widget(QTabWidget):
    def __init__(self, parent = None):
        super(Widget, self).__init__(parent)
        pass
if __name__ == '__main__': 
    app = QApplication(sys.argv)
    dialog = login()   
    if dialog.exec_():
        win=Widget()
        win.show()
        sys.exit(app.exec_())
        #sys.exit()     

这里写图片描述

猜你喜欢

转载自blog.csdn.net/qq_38641985/article/details/82152785