Advertisement
go6odn28

email_tk_inter

Jun 9th, 2024
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.66 KB | None | 0 0
  1. import tkinter as tk
  2.  
  3. def validate_email():
  4.     valid_label.place_forget()
  5.     username_error.place_forget()
  6.     symbol_at_error.place_forget()
  7.     domain_error.place_forget()
  8.     more_at_error.place_forget()
  9.  
  10.     email = input_email.get()
  11.  
  12.     possible_errors = [
  13.         (len(email.split('@')[0]) < MIN_SYMBOLS_USERNAME, username_error),
  14.         ('@' not in email, symbol_at_error),
  15.         (email.count('@') > 1, more_at_error),
  16.         (email.split('.')[-1] not in VALID_DOMAINS, domain_error)
  17.     ]
  18.  
  19.     for condition, error_label in possible_errors:
  20.         if condition:
  21.             error_label.place(x=150, y=50)
  22.             return
  23.  
  24.     valid_label.place(x=210, y=50)
  25.  
  26.  
  27. MIN_SYMBOLS_USERNAME = 5
  28. VALID_DOMAINS = ('com', 'bg', 'net', 'org')
  29.  
  30. window = tk.Tk()
  31. window.title('Email Validator')
  32. window.geometry('500x100')
  33.  
  34. entry_label = tk.Label(text='Enter your email:', fg='#463EAC')
  35. entry_label.pack()
  36.  
  37. input_email = tk.Entry(window, width=15)
  38. input_email.place(x=200, y=20)
  39.  
  40. generate_button = tk.Button(window, text='Validate', height=1, command=validate_email, fg='white', bg='blue')
  41. generate_button.place(x=300, y=18)
  42.  
  43. # Define the error messages
  44. username_error = tk.Label(window, text='Username must be more than 4 characters.\nTry again!', fg='Red')
  45. symbol_at_error = tk.Label(window, text='Email must contain @!', fg='Red')
  46. more_at_error = tk.Label(window, text='Email must contain only one @!', fg='Red')
  47. domain_error = tk.Label(window, text=f'Domain must be one of the following: {", ".join("." + domain for domain in VALID_DOMAINS)}', fg='Red')
  48.  
  49. valid_label = tk.Label(window, text='Email is valid!', fg='Blue')
  50.  
  51. window.mainloop()
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement