首页 编程教程正文

PyQt5初步(2)——本地登录页面

piaodoo 编程教程 2020-02-22 22:17:16 1879 0 python教程

本文来源吾爱破解论坛

成品及源码:链接:https://pan.baidu.com/s/1aQdD7xxtoqp1WAmWXlnZPA 提取码:4t9z



源码:
main.py内容
[Python] 纯文本查看 复制代码

import sys,os,json,re
from PyQt5.QtWidgets import QApplication, QMainWindow
from functools import partial
import hashlib
import login

path = os.path.dirname(os.path.realpath(__file__))+'\\'
try:
with open(path+'users.txt','r') as f:
pass
except:
with open(path+'users.txt','w') as f:
f.write('91 93')
def en_decode(txt,mode):
wi1 = "'[,0] \""
wi2 = 'OCP0L%@'
if mode == 'en':
return txt.translate(str.maketrans(wi1,wi2))
else:
return txt.translate(str.maketrans(wi2,wi1))

def acsii(txt,mode):
if mode=='en':
return ' '.join([str(ord(i)) for i in txt])
else:
a=''
txt = re.sub('\\n',' ',txt)
txt = txt.split(' ')
#print(txt)
for i in txt:
a+=chr(int(i))
return a

def sign_in(ui):
user = str(ui.lineEdit_user.text())
psw = str(ui.lineEdit_psw.text())
if len(psw) < 6:
ui.textBrowser.setText('密码至少6位!')
return False

h1 = hashlib.md5()
h1.update(user.encode(encoding='utf-8'))#
h2 = hashlib.md5()
h2.update(psw.encode(encoding='utf-8'))#.hexdigest()

with open(path+'users.txt','r') as f:
users = str(f.read())
##print(users)

users = acsii(users,'de')
#print(users)

users = en_decode(users,'de')
#print(users)

user_json = json.loads(users.replace('\'','"'))
#print(type(user_json),user_json)
user_json.append(["{}".format(h1.hexdigest()),"{}".format(h2.hexdigest())])
#print(user_json)
list1 = []
for i in user_json:
if i not in list1:
list1.append(i)
user_json=list1

user_json = acsii(str(user_json),'en')
#user_json = [ord(i) for i in str(user_json)]
#user_json = ' '.join(user_json)
#print(user_json)



with open(path+'users.txt','w') as f:
f.write(user_json)
#print(user_json)
ui.textBrowser.setText('注册成功!您的信息为\n账号:{}\n密码:{}'.format(user,psw))
ui.lineEdit_user.setText('')
ui.lineEdit_psw.setText('')



def check(ui):
user = str(ui.lineEdit_user.text())
psw = str(ui.lineEdit_psw.text())

h1 = hashlib.md5()
h1.update(user.encode(encoding='utf-8'))#
h2 = hashlib.md5()
h2.update(psw.encode(encoding='utf-8'))

with open(path+'users.txt','r') as f:
users = str(f.read())
#print(users)
users = acsii(users,'de')
users = en_decode(users,'de')
user_json = json.loads(users.replace('\'','"'))

in_result = [h1.hexdigest(),h2.hexdigest()]
#print(in_result)

if in_result in user_json:
#print('Success!')
ui.textBrowser.setText(user+',欢迎回来!')
ui.lineEdit_user.setText('')
ui.lineEdit_psw.setText('')
else:
ui.textBrowser.setText('Failed!')

if __name__ == '__main__':
app = QApplication(sys.argv)
MainWindow = QMainWindow()
ui = login.Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
MainWindow.setFixedSize(MainWindow.width(), MainWindow.height())
ui.LoginButton.clicked.connect(partial(check, ui))
ui.SignInButton.clicked.connect(partial(sign_in, ui))

sys.exit(app.exec_())


login.py文件内容
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-

# Form implementation generated from reading ui file 'login.ui'
#
# Created by: PyQt5 UI code generator 5.13.0
#
# WARNING! All changes made in this file will be lost!


from PyQt5 import QtCore, QtGui, QtWidgets


class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(359, 127)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.lineEdit_user = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_user.setCursor(QtGui.QCursor(QtCore.Qt.IBeamCursor))
self.lineEdit_user.setObjectName("lineEdit_user")
self.gridLayout.addWidget(self.lineEdit_user, 0, 1, 1, 1)
self.label_psw = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(14)
self.label_psw.setFont(font)
self.label_psw.setObjectName("label_psw")
self.gridLayout.addWidget(self.label_psw, 1, 0, 1, 1)
self.lineEdit_psw = QtWidgets.QLineEdit(self.centralwidget)
self.lineEdit_psw.setObjectName("lineEdit_psw")
self.gridLayout.addWidget(self.lineEdit_psw, 1, 1, 1, 1)
self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
self.textBrowser.setObjectName("textBrowser")
self.gridLayout.addWidget(self.textBrowser, 0, 2, 3, 1)
self.label_user = QtWidgets.QLabel(self.centralwidget)
font = QtGui.QFont()
font.setFamily("微软雅黑")
font.setPointSize(14)
self.label_user.setFont(font)
self.label_user.setObjectName("label_user")
self.gridLayout.addWidget(self.label_user, 0, 0, 1, 1)
self.LoginButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(13)
self.LoginButton.setFont(font)
self.LoginButton.setObjectName("LoginButton")
self.gridLayout.addWidget(self.LoginButton, 2, 0, 1, 1)
self.SignInButton = QtWidgets.QPushButton(self.centralwidget)
font = QtGui.QFont()
font.setFamily("黑体")
font.setPointSize(12)
self.SignInButton.setFont(font)
self.SignInButton.setObjectName("SignInButton")
self.gridLayout.addWidget(self.SignInButton, 2, 1, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)

self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)

def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "Login"))
self.label_psw.setText(_translate("MainWindow", "密码"))
self.label_user.setText(_translate("MainWindow", "用户名"))
self.LoginButton.setText(_translate("MainWindow", "登录"))
self.SignInButton.setText(_translate("MainWindow", "注册"))

版权声明:

本站所有资源均为站长或网友整理自互联网或站长购买自互联网,站长无法分辨资源版权出自何处,所以不承担任何版权以及其他问题带来的法律责任,如有侵权或者其他问题请联系站长删除!站长QQ754403226 谢谢。

有关影视版权:本站只供百度云网盘资源,版权均属于影片公司所有,请在下载后24小时删除,切勿用于商业用途。本站所有资源信息均从互联网搜索而来,本站不对显示的内容承担责任,如您认为本站页面信息侵犯了您的权益,请附上版权证明邮件告知【754403226@qq.com】,在收到邮件后72小时内删除。本文链接:https://www.piaodoo.com/8117.html

搜索