Home > Net >  How to make a tkinter entry widget for email only
How to make a tkinter entry widget for email only

Time:04-09

I am building a simple login system using Tkinter in python, I want to take user email id by validating the user type value should be a email not anything else. How to make that, please any one tell me.

This is the simple version of my code,

from tkinter import *

root = Tk()
root.geometry("400x300")


def signIn():

    if entry_var2.get().isdigit():
        print("emailId should not have only numbers")
    elif entry_var2.get().isalpha():
        print("emailId shold have not only alphabets")
    elif entry_var2.get().isspace():
        print("emailId don't have space")
    else:
        print("Login Success") # But it gives not the output I want


# username
entry_var1 = StringVar()
entry1 = Entry(root, textvariable = entry_var1)
entry1.grid(row=0, column=0)

# email id
entry_var2 = StringVar()
entry2 = Entry(root, textvariable = entry_var2)
entry2.grid(row=0, column=0)

# Sign In
button = Button(root, text="Result", command=signIn)
button.grid(row=1, column=0)

root.mainloop()

CodePudding user response:

Use regex to check for email strings.

import re

regex = r'\b[A-Za-z0-9._% -] @[A-Za-z0-9.-] \.[A-Z|a-z]{2,}\b'
 
 
def signIn():
    if(re.fullmatch(regex, entry_var2.get())):
        print("Valid Email")
    else:
        print("Invalid Email")

CodePudding user response:

this is python function to validate email using regex:

import re
 
regex = r'\b[A-Za-z0-9._% -] @[A-Za-z0-9.-] \.[A-Z|a-z]{2,}\b'

def check(entry_var2):
    if(re.fullmatch(regex, entry_var2)):
        print("Valid Email")

    else:
        print("Invalid Email")

for full solution look at this article

  • Related