├── README.md └── hedospace-security ├── refresh.png └── main.py /README.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /hedospace-security/refresh.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sahandmohammadrehzaii/hedospace-security/HEAD/hedospace-security/refresh.png -------------------------------------------------------------------------------- /hedospace-security/main.py: -------------------------------------------------------------------------------- 1 | # Importing the Required Libraries 2 | import random 3 | import string 4 | from tkinter import Tk, Label, Entry, Button, END 5 | from PIL import ImageTk, Image 6 | from captcha.image import ImageCaptcha 7 | 8 | 9 | def createImage(flag=0): 10 | """ 11 | Defining the method createImage() which will create 12 | and generate a Captcha Image based on a randomly 13 | generated strings. The Captcha Image generated is then 14 | incorporated into the GUI window we have designed. 15 | """ 16 | global random_string 17 | global image_label 18 | global image_display 19 | global entry 20 | global verify_label 21 | 22 | # The if block below works only when we press the 23 | # Reload Button in the GUI. It basically removes 24 | # the label (if visible) which shows whether the 25 | # entered string is correct or incorrect. 26 | if flag == 1: 27 | verify_label.grid_forget() 28 | 29 | # Removing the contents of the input box. 30 | entry.delete(0, END) 31 | 32 | # Generating a random string for the Captcha 33 | random_string = ''.join(random.choices(string.ascii_letters + string.digits, k=6)) 34 | 35 | # Creating a Captcha Image 36 | image_captcha = ImageCaptcha(width=250, height=125) 37 | image_generated = image_captcha.generate(random_string) 38 | image_display = ImageTk.PhotoImage(Image.open(image_generated)) 39 | 40 | # Removing the previous Image (if present) and 41 | # displaying a new one. 42 | image_label.grid_forget() 43 | image_label = Label(root, image=image_display) 44 | image_label.grid(row=1, column=0, columnspan=2, padx=10) 45 | 46 | 47 | def check(x, y): 48 | """ 49 | Defining the method check() which will check 50 | whether the string entered by the user matches 51 | with the randomly generated string. If there is 52 | a match then "Verified" pops up in the window. 53 | Otherwise, "Incorrect!" pops up and a new Captcha 54 | Image is generated for the user to try again. 55 | """ 56 | 57 | # Making the scope of the below mentioned 58 | # variables because their values are accessed 59 | # globally in this script. 60 | global verify_label 61 | 62 | verify_label.grid_forget() 63 | 64 | if x.lower() == y.lower(): 65 | verify_label = Label(master=root, 66 | text="Verified", 67 | font="Arial 15", 68 | bg='#ffe75c', 69 | fg="#00a806" 70 | ) 71 | verify_label.grid(row=0, column=0, columnspan=2, pady=10) 72 | else: 73 | verify_label = Label(master=root, 74 | text="Incorrect!", 75 | font="Arial 15", 76 | bg='#ffe75c', 77 | fg="#fa0800" 78 | ) 79 | verify_label.grid(row=0, column=0, columnspan=2, pady=10) 80 | createImage() 81 | 82 | 83 | if __name__ == "__main__": 84 | # Initializing Tkinter by creating a root widget, 85 | # setting Title and Background Color 86 | root = Tk() 87 | root.title('Image Captcha') 88 | root.configure(background='#ffe75c') 89 | 90 | # Initializing the Variables to be defined later 91 | verify_label = Label(root) 92 | image_label = Label(root) 93 | 94 | # Defining the Input Box and placing it in the window 95 | entry = Entry(root, width=10, borderwidth=5, 96 | font="Arial 15", justify="center") 97 | entry.grid(row=2, column=0) 98 | 99 | # Creating an Image for the first time. 100 | createImage() 101 | 102 | # Defining the path for the reload button image 103 | # and using it to add the reload button in the 104 | # GUI window 105 | reload_img = ImageTk.PhotoImage(Image.open("refresh.png").resize((32, 32), resample=Image.LANCZOS)) 106 | reload_button = Button(image=reload_img, command=lambda: createImage(1)) 107 | reload_button.grid(row=2, column=1, pady=10) 108 | 109 | # Defining the submit button 110 | submit_button = Button(root, text="Submit", font="Arial 10", command=lambda: check(entry.get(), random_string)) 111 | submit_button.grid(row=3, column=0, columnspan=2, pady=10) 112 | root.bind('', func=lambda Event: check(entry.get(), random_string)) 113 | 114 | # This makes the program loops till the user 115 | # doesn't close the GUI window 116 | root.mainloop() --------------------------------------------------------------------------------