Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- def validate_email():
- valid_label.place_forget()
- username_error.place_forget()
- symbol_at_error.place_forget()
- domain_error.place_forget()
- more_at_error.place_forget()
- email = input_email.get()
- possible_errors = [
- (len(email.split('@')[0]) < MIN_SYMBOLS_USERNAME, username_error),
- ('@' not in email, symbol_at_error),
- (email.count('@') > 1, more_at_error),
- (email.split('.')[-1] not in VALID_DOMAINS, domain_error)
- ]
- for condition, error_label in possible_errors:
- if condition:
- error_label.place(x=150, y=50)
- return
- valid_label.place(x=210, y=50)
- MIN_SYMBOLS_USERNAME = 5
- VALID_DOMAINS = ('com', 'bg', 'net', 'org')
- window = tk.Tk()
- window.title('Email Validator')
- window.geometry('500x100')
- entry_label = tk.Label(text='Enter your email:', fg='#463EAC')
- entry_label.pack()
- input_email = tk.Entry(window, width=15)
- input_email.place(x=200, y=20)
- generate_button = tk.Button(window, text='Validate', height=1, command=validate_email, fg='white', bg='blue')
- generate_button.place(x=300, y=18)
- # Define the error messages
- username_error = tk.Label(window, text='Username must be more than 4 characters.\nTry again!', fg='Red')
- symbol_at_error = tk.Label(window, text='Email must contain @!', fg='Red')
- more_at_error = tk.Label(window, text='Email must contain only one @!', fg='Red')
- domain_error = tk.Label(window, text=f'Domain must be one of the following: {", ".join("." + domain for domain in VALID_DOMAINS)}', fg='Red')
- valid_label = tk.Label(window, text='Email is valid!', fg='Blue')
- window.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement