用pyqt完成动态验证码

在这里插入图片描述
在这里插入图片描述

# -*- coding: utf-8 -*-

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

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox
import sys
import zhenzismsclient as smsclient
import random


class Ui_Dialog(object):
    def setupUi(self, Dialog):
        Dialog.setObjectName("Dialog")
        Dialog.resize(891, 618)
        self.LineEdit_phoneNumber = QtWidgets.QLineEdit(Dialog)
        self.LineEdit_phoneNumber.setGeometry(QtCore.QRect(300, 200, 171, 31))
        self.LineEdit_phoneNumber.setObjectName("LineEdit_phoneNumber")
        self.label = QtWidgets.QLabel(Dialog)
        self.label.setGeometry(QtCore.QRect(170, 210, 54, 12))
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(Dialog)
        self.label_2.setGeometry(QtCore.QRect(170, 250, 54, 12))
        self.label_2.setObjectName("label_2")
        self.Btn_sendMessage = QtWidgets.QPushButton(Dialog)
        self.Btn_sendMessage.setGeometry(QtCore.QRect(230, 320, 75, 23))
        self.Btn_sendMessage.setObjectName("Btn_sendMessage")
        self.Btn_submitCode = QtWidgets.QPushButton(Dialog)
        self.Btn_submitCode.setGeometry(QtCore.QRect(390, 320, 75, 23))
        self.Btn_submitCode.setObjectName("Btn_submitCode")
        self.LineEdit_messageCode = QtWidgets.QLineEdit(Dialog)
        self.LineEdit_messageCode.setGeometry(QtCore.QRect(300, 240, 171, 31))
        self.LineEdit_messageCode.setObjectName("LineEdit_messageCode")

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

    def retranslateUi(self, Dialog):
        _translate = QtCore.QCoreApplication.translate
        Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
        self.label.setText(_translate("Dialog", "手机号:"))
        self.label_2.setText(_translate("Dialog", "验证码:"))
        self.Btn_sendMessage.setText(_translate("Dialog", "发送验证码"))
        self.Btn_submitCode.setText(_translate("Dialog", "提交"))

class logForm(QMainWindow, Ui_Dialog):
    def __init__(self, parent=None):
        super(logForm, self).__init__(parent)
        self.setupUi(self)
        self.Btn_sendMessage.clicked.connect(self.sendMessage)
        self.Btn_submitCode.clicked.connect(self.checkCode)
        self.code = ''

    #当用户输入手机号后,点击发送验证码按钮,就调用此函数发送验证码
    def sendMessage(self):
        client = smsclient.ZhenziSmsClient("https://sms_developer.zhenzikj.com", "...","423ae9c6-5082-4e05-b4ec-478999b25ee8")#保密
        for num in range(1, 5):
            self.code = self.code + str(random.randint(0, 9))
        phoneNum = self.LineEdit_phoneNumber.text()
        params = {'message': '医我所爱,您的验证码为:' + self.code, 'number': phoneNum}
        result = client.send(params)
        #if result["code"] != 0:
             #print(result)

    #验证用户输入的验证码是否正确
    def checkCode(self):
        user_Inputcode = self.LineEdit_messageCode.text()
        print(user_Inputcode)
        print(type(user_Inputcode))
        print(self.code)
        print(type(self.code))
        if user_Inputcode == self.code:
            #这里用户身份验证成功,就可以跳出其他窗口
            print("success")
        else:
            #这里就提示错误信息
            print("error")


if __name__ == "__main__":
    app = QApplication(sys.argv)
    myWin = logForm()
    myWin.show()
    sys.exit(app.exec_())
    # app = QtWidgets.QApplication(sys.argv)
    # myshow = QtWidgets.QMainWindow()
    # ui = Ui_Dialog()
    # ui.setupUi(myshow)
    # myshow.show()
    # sys.exit(app.exec_())
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值