Nov-23-2019, 09:56 PM
First, I would like to thank Buran for his support.
Secondly, attached is the working code for anyone whom is interested. I have removed "create user" option for simplicity.
Secondly, attached is the working code for anyone whom is interested. I have removed "create user" option for simplicity.
#imports
#don't forget to dos>pip install mysql-connector-python
from tkinter import *
from tkinter import messagebox as ms
import mysql.connector as mysql
mydb = mysql.connect(
host="localhost",
user="goktan",
passwd="nopassword64",
database="testdb"
)
#main Class
class main:
def __init__(self,master):
# Window
self.master = master
# Some Usefull variables
self.username = StringVar()
self.password = StringVar()
self.n_username = StringVar()
self.n_password = StringVar()
#Create Widgets
self.widgets()
#Login Function
def login(self):
#Establish Connection
cursor = mydb.cursor()
#Find user If there is any take proper action
find_user = ("SELECT * FROM user WHERE username = %s and password = %s")
cursor.execute(find_user, [(self.username.get()), (self.password.get())])
result = cursor.fetchall()
if result:
self.logf.pack_forget()
self.head['text'] = self.username.get() + '\n Loged In'
self.head['pady'] = 150
else:
ms.showerror('Oops!','Username Not Found.')
#Frame Packing Methords
def log(self):
self.username.set('')
self.password.set('')
self.crf.pack_forget()
self.head['text'] = 'LOGIN'
self.logf.pack()
#Draw Widgets
def widgets(self):
self.head = Label(self.master,text = 'LOGIN',font = ('',35),pady = 10)
self.head.pack()
self.logf = Frame(self.master,padx =10,pady = 10)
Label(self.logf,text = 'Username: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=0,column=1)
Label(self.logf,text = 'Password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
Button(self.logf,text = ' Login ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.login).grid()
self.logf.pack()
#create window and application object
root = Tk()
root.title("Login Form")
main(root)
root.mainloop()
