
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/my_database")
#my_conn = create_engine("sqlite:///my_db_mem.db") # SQLite database
my_query="INSERT INTO mem(name,userid,password) values(%s,%s,%s)"
In case of SQLite we have to change the query like this.
my_query="INSERT INTO mem(name,userid,password) values(?,?,?)"
In all places we have to change the query part using this. No need to change any other part of the script.
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:pw@localhost/my_db")
#my_conn = create_engine("sqlite:///my_db_mem.db")
import tkinter as tk
from tkinter import ttk
from tkinter import *
my_w = tk.Tk()
my_w.geometry("200x200")
b1 = tk.Button(my_w, text='Register', width=20,command=lambda:my_registration())
b1.grid(row=2,column=2)
b2 = tk.Button(my_w, text='Login', width=20,command=lambda:my_login())
b2.grid(row=3,column=2)
b3 = tk.Button(my_w, text='User List', width=20,command=lambda:user_list())
b3.grid(row=4,column=2)
#### End of main window ####
def my_registration():
my_w_child=Toplevel(my_w) # Child window
my_w_child.geometry("300x200") # Size of the window
my_w_child.title("www.plus2net.com")
my_str1 = tk.StringVar()
l1 = tk.Label(my_w_child, textvariable=my_str1 )
l1.grid(row=1,column=2,columnspan=2,sticky='E')
my_str1.set("-Registration--")
my_str = tk.StringVar()
l1 = tk.Label(my_w_child, textvariable=my_str,width=15 )
l1.grid(row=2,column=2)
my_str.set("Name")
name1 = tk.Entry(my_w_child, width=15) # added one Entry box
name1.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("Userid")
userid2 = tk.Entry(my_w_child, width=15) # added one Entry box
userid2.grid(row=3,column=3)
my_str3 = tk.StringVar()
l3 = tk.Label(my_w_child, textvariable=my_str3 )
l3.grid(row=4,column=2)
my_str3.set("Password")
pw3 = tk.Entry(my_w_child, width=15,show='*') # added one Entry box
pw3.grid(row=4,column=3)
b4 = tk.Button(my_w_child, text='Register', command=lambda:add_data())
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,sticky='W')
my_msg.set("Add data")
## layout is over , now adding data to table
def add_data():
flag_validation=True # set the flag
my_name=name1.get() # read name
userid=userid2.get() # read userid
pw=pw3.get() # read password
msg = ""
# validation of entered data
if(len(my_name) <=3): # length of name minimum 4 char
flag_validation=False
msg = msg + "Name minimum 4 char \n "
if(len(pw) <6): # length of name minimum 4 char
flag_validation=False
msg = msg + "password must be minimum 6 char \n "
if(len(userid) <5 or len(userid)>12 or not userid.isalnum()): # userid between 5 and 12
flag_validation=False
msg = msg + "Userid should not be less than 5 \n and more than 12 \n Special chars not allowed "
else:
#my_data=(userid,)
#print(type(my_data))
#my_query="SELECT * FROM mem WHERE userid='" + userid + "'"
my_query="SELECT * FROM mem WHERE userid=%s" #? for sqlite
r_set=my_conn.execute(my_query,userid)
no=len(r_set.fetchall())
if(no>0):
flag_validation=False
msg = msg + "userid exists select different one "
if(flag_validation):
try:
my_msg.set("Adding data...")
my_data1=(my_name,userid,pw)
my_query="INSERT INTO mem(name,userid,password) values(%s,%s,%s)"
curs=my_conn.execute(my_query,my_data1)
#my_conn.commit()
#x=my_conn.execute('''select last_insert_rowid()''')
#id=x.fetchone()
msg="Record added, ID : "+ str(curs.lastrowid)
name1.delete(0,END) # delete the entered text
userid2.delete(0,END) # delete the entered text
pw3.delete(0,END) # delete the entered text
except Exception as e:
print(e)
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 of Registration process ####
### Start of Login process & window ######
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 #####
##### Listing users with select and delete ###
def user_list():
my_w_child3=Toplevel(my_w) # Child window
my_w_child3.geometry("300x300") # Size of the window
my_w_child3.title("www.plus2net.com")
from tkinter import messagebox as msg
# display the list
def my_show():
r_set=my_conn.execute('''SELECT * from mem LIMIT 0,10''');
trv=ttk.Treeview(my_w_child3,selectmode='browse')
trv.grid(row=1,column=1,padx=20,pady=20)
trv["columns"]=("1","2","3","4")
trv['show']='headings'
trv.column("1",width=30,anchor='c')
trv.column("2",width=80,anchor='c')
trv.column("3",width=80,anchor='c')
trv.column("4",width=80,anchor='c')
trv.heading("1",text="id")
trv.heading("2",text="Name")
trv.heading("3",text="Userid")
trv.heading("4",text="Password")
for dt in r_set:
trv.insert("",'end',iid=dt[0],
values=(dt[0],dt[1],dt[2],dt[3]))
b1 = tk.Button(my_w_child3, text='delete row', width=20,bg='yellow',command=lambda: delete())
b1.grid(row=2,column=1)
def delete():
selected_item = trv.selection()[0]
my_var=msg.askyesnocancel("Delete ?","Delete id:"+str(selected_item),icon='warning',default='no',parent=my_w_child3)
if my_var: # True if yes button is clicked
query="DELETE FROM mem WHERE id=%s"
rs=my_conn.execute(query,selected_item)
if(rs.rowcount==1):
trv.delete(selected_item)
msg.showerror("Deleted ","No of records deleted = " + str(rs.rowcount),parent=my_w_child3)
#my_conn.commit()
my_show()
my_w.mainloop() # Keep the window open
Member Registration
Member Login
Member Listing
CREATE TABLE IF NOT EXISTS `mem` (
`id` int(3) NOT NULL AUTO_INCREMENT,
`name` varchar(50) CHARACTER SET utf8 NOT NULL DEFAULT '',
`userid` varchar(15) CHARACTER SET utf8 NOT NULL DEFAULT '',
`password` varchar(20) NOT NULL DEFAULT '',
UNIQUE KEY `id` (`id`),
UNIQUE KEY `usrid` (`userid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=6 ;
--
-- Dumping data for table `mem`
--
INSERT INTO `mem` (`id`, `name`, `userid`, `password`) VALUES
(1, 'abc1', 'abc123', 'test123'),
(2, 'abc2', 'abc213', 'test213'),
(3, 'abc3', 'abc312', 'test312');
Displaying records from student table
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.