« Member Login System
This is Part 3 of three part script. There are three windows for this.
Part 1 : Adding members to our sample table ( Registration window )
Part 2 : Login window, Checking userid and password and showing message accordingly.
Part 3 : Listing all members of the table with button to delete any member
- Connect to MySQL database
-
We have one mem table with three sample records having userid, password and name columns.
-
Create one Tkinter window to ask user to enter userid and password.
-
Build the query and check the userid and password against the mem table and display appropriate message.
Connection to MySQL database & sample table
We used SQLAlchemy to connect to MySQL database.
my_conn = create_engine("mysql+mysqldb://root:test@localhost/my_db")
#my_conn = create_engine("sqlite:///my_db_mem.db")
Connect to MySQL database
Use the SQL dump at the end of this page to create mem table with sample data. Here is the sample data.
id | name | userid | password |
1 | smo1 | smo123 | test123 |
2 | smo2 | smo213 | test213 |
3 | smo3 | smo312 | test312 |
### Start of Login window & process ######
def my_login():
my_w_child=Toplevel(my_w) # Child window
my_w_child.geometry("220x140") # Size of the window
my_w_child.title("www.plus2net.com")
my_str1 = tk.StringVar()
my_font1=('times', 18, 'bold')
l1 = tk.Label(my_w_child, textvariable=my_str1,font=my_font1 )
l1.grid(row=1,column=2,columnspan=2)
my_str1.set("Login")
my_str = tk.StringVar()
l1 = tk.Label(my_w_child, textvariable=my_str,width=10 )
l1.grid(row=2,column=2)
my_str.set("Userid")
userid_t = tk.Entry(my_w_child, width=17) # added one Entry box
userid_t.grid(row=2,column=3)
my_str2 = tk.StringVar()
l2 = tk.Label(my_w_child, textvariable=my_str2 )
l2.grid(row=3,column=2)
my_str2.set("Password")
pw_t = tk.Entry(my_w_child, width=17) # added one Entry box
pw_t.grid(row=3,column=3)
b4 = tk.Button(my_w_child, text='Login', command=lambda:login())
b4.grid(row=5,column=3)
my_msg = tk.StringVar()
l5 = tk.Label(my_w_child, textvariable=my_msg)
l5.grid(row=6,column=1,columnspan=4)
my_msg.set("Message here ")
## End of design of login window ##
def login():
flag_validation=True # set the flag
userid=userid_t.get() # read userid
pw=pw_t.get() # read pw
msg = ""
# validation of entered data
if(len(pw) <6): # length of name minimum 4 char
flag_validation=False
msg = msg + "Password mininum 6 char \n "
# userid between 5 and 12
if(len(userid) <5 or len(userid)>12 or not userid.isalnum()):
flag_validation=False
msg = msg + """Userid should not be less than 5 \n
& more than 12 \n Special chars not allowed"""
else:
my_query="SELECT password,name FROM mem WHERE userid=%s"
r_set=my_conn.execute(my_query,userid)
r_set=r_set.fetchall()
no=len(r_set)
if(no != 1):
flag_validation=False
msg = msg + "userid is not available "
elif(r_set[0][0] != pw):
flag_validation=False
#print(r_set[0][0]) # print password
msg = msg + "Wrong Password "
else:
msg = "Welcome " + r_set[0][1]
if(flag_validation):
l5.config(fg='green')
else:
l5.config(fg='red')
my_msg.set(msg)
msg=''
my_w_child.after(3000, lambda: my_msg.set(''))
##### end login window #####
There are other two parts, one for listing and other for registration.
Main Login script with three functions
Member Registration
Member Listing
Download login-system.ipynb file ( zip )⇓
← Subscribe to our YouTube Channel here