python-产品篇-图形验证码

文章目录

准备

在这里插入图片描述

代码


from tkinter import *
from tkinter.messagebox import *
import time
import os
root = Tk()
import random
dirall=[]
randima =[]

def call():
    global  image1
    global  image2
    global  image3
    global  image4
    global  image5
    global  selone
    global  seltwo
    dirall.clear()
    randima.clear()
    for dirs in os.walk('./test/'):
        dirall.append(dirs)
    secdir=dirall[0][1]
    luckima=random.choice(secdir)
    secdir.remove(luckima)
    decdir=luckima
    filelist=os.listdir('./test/'+decdir)
    selone = random.choice(filelist)
    randima.append(decdir+'/'+selone)
    filelist.remove(selone)
    seltwo =  random.choice(filelist)
    randima.append(decdir+'/'+seltwo)
    for i in range(3):
       randdir=random.choice(secdir)
       randfile=os.listdir('./test/'+randdir)
       randima.append(randdir+'/'+random.choice(randfile))
       secdir.remove(randdir)
    random.shuffle(randima)
 
    title=Label(root, text="所有的"+luckima).grid(column=1, row=0) 
    image1 = PhotoImage(file='test/'+randima[0])
    image2 = PhotoImage(file='test/'+randima[1])
    image3 = PhotoImage(file='test/'+randima[2])
    image4 = PhotoImage(file='test/'+randima[3])
    image5 = PhotoImage(file='test/'+randima[4])
    label = Label(root,image=image1).grid(column=0, row=1) 
    labe2 = Label(root,image=image2).grid(column=1, row=1) 
    labe3 = Label(root,image=image3).grid(column=2, row=1) 
    labe4 = Label(root,image=image4).grid(column=3, row=1) 
    labe5 = Label(root,image=image5).grid(column=4, row=1) 
def defcheck():
    cvar1.set(0)
    cvar2.set(0)
    cvar3.set(0)
    cvar4.set(0)
    cvar5.set(0)
def test():
    count=0
    if cvar1.get()==1:
        if selone not in randima[0] and seltwo not in randima[0]:
            warn=showwarning(title='图形验证码', message ='验证失败,请重新验证!' )
            count=20            
            call()
        else:
            count +=1
    if cvar2.get()==1 and count!=20:
        if selone not in randima[1] and seltwo not in randima[1]:
            warn=showwarning(title='图形验证码', message ='验证失败,请重新验证!' )
            count=20
            call()
        else:
            count+=1
    if cvar3.get()==1 and count!=20:
        if selone not in randima[2] and seltwo not in randima[2]:
            warn=showwarning(title='图形验证码', message ='验证失败,请重新验证!' )
            count=20
            call()
        else:
            count+=1
    if cvar4.get()==1 and count!=20:
        if selone not in randima[3] and seltwo not in randima[3]:
            warn=showwarning(title='图形验证码', message ='验证失败,请重新验证!' )
            count=20
            call()
        else:
            count +=1

    if cvar5.get()==1 and count!=20:
        if selone not in randima[4] and seltwo not in randima[4]:
            warn=showwarning(title='图形验证码', message ='验证失败,请重新验证!' )
            count=20
            call()
        else:
            count +=1
    if count==2 :
        mess=showwarning(title='图形验证码', message ='验证通过,将进入系统!' )
        root.destroy()  
    elif count==0:
        mess=showwarning(title='图形验证码', message ='请选择图形再验证!' )
    else:
        defcheck()
            
root.title('图形验证码')
root.wm_attributes('-topmost', 1)
root.geometry('500x180')
title=Label(root, text="请点击下面图中").grid(column=0, row=0)

call()



cvar1 = IntVar()
cvar2 = IntVar()
cvar3 = IntVar()
cvar4 = IntVar()
cvar5 = IntVar()
check1 = Checkbutton(root, text = "选择", variable = cvar1, onvalue = 1, offvalue = 0).grid(column=0, row=2) 
check2= Checkbutton(root, text = "选择", variable = cvar2,onvalue = 1, offvalue = 0).grid(column=1, row=2) 
check3 = Checkbutton(root, text = "选择", variable = cvar3, onvalue = 1, offvalue = 0).grid(column=2, row=2) 
check4= Checkbutton(root, text = "选择", variable = cvar4,onvalue = 1, offvalue = 0).grid(column=3, row=2)
check5 = Checkbutton(root, text = "选择", variable = cvar5, onvalue = 1, offvalue = 0).grid(column=4, row=2)
actis = Button(root, text="确认", command=test,width=8)      
actis.grid(column=3, row=3)    
actcal = Button(root, text="换一组", command=call,width=8)      
actcal.grid(column=4, row=3)   
root.mainloop()

效果

在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

fo安方

觉得俺的文章还行,感谢打赏,爱

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

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

打赏作者

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

抵扣说明:

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

余额充值