<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Satyam Kulkarni</title>
    <description>The latest articles on DEV Community by Satyam Kulkarni (@sk1122).</description>
    <link>https://dev.to/sk1122</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F347727%2F28262a2c-7267-4cc8-8322-485ef33b63fe.jpeg</url>
      <title>DEV Community: Satyam Kulkarni</title>
      <link>https://dev.to/sk1122</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sk1122"/>
    <language>en</language>
    <item>
      <title>A Simple Login System With Python &amp; Tkinter
</title>
      <dc:creator>Satyam Kulkarni</dc:creator>
      <pubDate>Sun, 11 Oct 2020 20:51:50 +0000</pubDate>
      <link>https://dev.to/sk1122/a-simple-login-system-with-python-tkinter-54do</link>
      <guid>https://dev.to/sk1122/a-simple-login-system-with-python-tkinter-54do</guid>
      <description>&lt;h2&gt;
  
  
  Hi Everyone, This Article is Explaining all about How to Build a Simple Login System with Python and Tkinter
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hpY_K5AB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/12000/0%2A3nzoNi8tXnJYJgeh" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hpY_K5AB--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/12000/0%2A3nzoNi8tXnJYJgeh" alt="Photo by [Maxwell Nelson](https://unsplash.com/@maxcodes?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)"&gt;&lt;/a&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@maxcodes?utm_source=medium&amp;amp;utm_medium=referral"&gt;Maxwell Nelson&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you want to check only Source Code,&lt;/p&gt;

&lt;p&gt;&lt;a href="https://github.com/sk1122"&gt;Github&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Meanings of Terms we will be using throughout this Text
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--d4SNwjl0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/11520/0%2ASA72iU4Cw26hMmZv" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--d4SNwjl0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/11520/0%2ASA72iU4Cw26hMmZv" alt="Photo by [Pisit Heng](https://unsplash.com/@pisitheng?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)"&gt;&lt;/a&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@pisitheng?utm_source=medium&amp;amp;utm_medium=referral"&gt;Pisit Heng&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;GUI&lt;/strong&gt; — Graphical User Interface&lt;/p&gt;

&lt;p&gt;It is the Screen that will appear with Interactive Text, Button, etc.&lt;/p&gt;

&lt;h2&gt;
  
  
  Let’s Begin
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--mXujowG8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/11342/0%2Ar8pvFWt8qclHcEh6" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--mXujowG8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/11342/0%2Ar8pvFWt8qclHcEh6" alt="Photo by [Kyle Glenn](https://unsplash.com/@kylejglenn?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)"&gt;&lt;/a&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@kylejglenn?utm_source=medium&amp;amp;utm_medium=referral"&gt;Kyle Glenn&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;This Login System is made with Tkinter, a Python In-Built Library for Building Cross-Platform GUI’s.&lt;/p&gt;

&lt;p&gt;It also uses some other Libraries like Bcrypt, for Encrypting Password and Sqlite3, a Python In-Built Library for Interacting with SQlite3 Databases.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fasten Your &lt;strong&gt;SeatBelt&lt;/strong&gt; Programmer
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E8VvPyh6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/11150/0%2A2nvcey4WptDk3t8k" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E8VvPyh6--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn-images-1.medium.com/max/11150/0%2A2nvcey4WptDk3t8k" alt="Photo by [Fabian Grohs](https://unsplash.com/@grohsfabian?utm_source=medium&amp;amp;utm_medium=referral) on [Unsplash](https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral)"&gt;&lt;/a&gt;&lt;em&gt;Photo by &lt;a href="https://unsplash.com/@grohsfabian?utm_source=medium&amp;amp;utm_medium=referral"&gt;Fabian Grohs&lt;/a&gt; on &lt;a href="https://unsplash.com?utm_source=medium&amp;amp;utm_medium=referral"&gt;Unsplash&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  First we will Install Required Libraries
&lt;/h3&gt;

&lt;p&gt;Tkinter — It is Pre Installed with Python, but for sometimes it does not come. It happened to me.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;sudo apt-get install python-tk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, Coming Towards Bcrypt and SQlite3.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;pip install bcrypt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;SQlite3 Comes Pre Installed&lt;/p&gt;

&lt;h3&gt;
  
  
  Structuring our main.py
&lt;/h3&gt;

&lt;p&gt;main.py is Main Entry Point for our App.&lt;/p&gt;

&lt;p&gt;We will be Using OOP Methodology to program our Login System&lt;/p&gt;

&lt;p&gt;Here,&lt;/p&gt;

&lt;p&gt;*Line 1, *Import all functions from Tkinter Library&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Line 3,&lt;/em&gt; Defines a Class called MainWindow for our Program&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Line 4&lt;/em&gt;, It is &lt;strong&gt;init&lt;/strong&gt;() Special Method for Classes which help to define local variables for Classes&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;init&lt;/strong&gt;() method, We define the app and make it an instance of Tk() class of Tkinter&lt;/p&gt;

&lt;p&gt;&lt;em&gt;And other stuff is,&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Title&lt;/strong&gt; — It is used to assign a Title to our Main Window&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Geometry&lt;/strong&gt; — It is a String used to define the Height and Width of our GUI&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Label&lt;/strong&gt; — It is used to print text on Screen or GUI&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Button&lt;/strong&gt; — It is used to create a Button&lt;/p&gt;

&lt;p&gt;Add these 2 lines below the above code to run it...&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;app = MainWindow()
app.run()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;And then go to terminal, and cd into that directory.&lt;/p&gt;

&lt;p&gt;Use the following command to run python file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd YourDirectory
python3 main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;On &lt;strong&gt;&lt;em&gt;Windows,&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;python main.py
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This code will produce an error, as some things need to be changed,&lt;/p&gt;

&lt;p&gt;Add a Login and register function in code&lt;/p&gt;

&lt;p&gt;The Code Should Look Something like this,&lt;/p&gt;

&lt;p&gt;from tkinter import *&lt;br&gt;
from login import Login, Register&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class MainWindow:
 def __init__(self):
 self.app = Tk()
 self.app.title(“Login with Python”)
 self.app.geometry(“300x250”)
 self.label = Label(self.app, text=”Welcome To App”)
 self.label.place(x=95, y=40)
 self.login = Button(self.app, text=”Login”,
 pady=5, padx=30, command=login)
 self.login.place(x=100, y=100)
 self.register = Button(self.app, text=”Register”,
 pady=5, padx=20, command=register)
 self.register.place(x=100, y=150)

def run(self):
 self.app.mainloop()

def login():
 loginTk = Login()
 loginTk.run()

def register():
 registerTk = Register()
 registerTk.run()

app = MainWindow()
app.run()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Now, You need to create a file named “login.py” to store Login and Register Class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;login.py&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from tkinter import *
from tkinter import messagebox
import bcrypt
from database import Database

db = Database()
db.createTable()

class Login:

“””
 Class for Login
 [@param](http://twitter.com/param) username
 [@param](http://twitter.com/param) password
 “””

def __init__(self):
 “””
 Class Init Method for GUI
 :params — loginWindow, label, username
 “””

# Variables for Tkinter
 self.loginWindow = Tk()
 self.loginWindow.title(“Login with Python”)
 self.loginWindow.geometry(“300x250”)
 self.label = Label(self.loginWindow, text=”Login”)
 self.label.place(x=95, y=40)

# Just Creepy Tkinter Stuff
 self.usernameS = StringVar()
 self.passwordS = StringVar()

self.usernameE = Entry(
 self.loginWindow, relief=FLAT, textvariable=self.usernameS)
 self.usernameE.place(x=70, y=80)
 self.passwordE = Entry(
 self.loginWindow, show=”*”, relief=FLAT, textvariable=self.passwordS)
 self.passwordE.place(x=70, y=120)

# Actual Variales
 self.username = self.usernameS.get()
 self.password = self.passwordS.get()

self.submit = Button(self.loginWindow, text=”Submit”,
 pady=5, padx=20, command=self.validate)
 self.submit.place(x=100, y=150)

def validate(self):
 data = (self.username,)
 inputData = (self.username, self.password,)
 try:
 if (db.validateData(data, inputData)):
 messagebox.showinfo(“Successful”, “Login Was Successful”)
 else:
 messagebox.showerror(“Error”, “Wrong Credentials”)
 except IndexError:
 messagebox.showerror(“Error”, “Wrong Credentials”)

def run(self):
 self.loginWindow.mainloop()

class Register:

“””
 Class for Register
 [@param](http://twitter.com/param) username
 [@param](http://twitter.com/param) password
 “””

def __init__(self):

self.registerWindow = Tk()
 self.registerWindow.title(“Register with Python”)
 self.registerWindow.geometry(“300x250”)
 self.label = Label(self.registerWindow, text=”Register”)
 self.label.place(x=95, y=40)

# Just Creepy Tkinter Stuff
 self.usernameS = StringVar()
 self.passwordS = StringVar()

self.usernameE = Entry(self.registerWindow,
 relief=FLAT, textvariable=self.usernameS)
 self.usernameE.place(x=70, y=80)
 self.passwordE = Entry(self.registerWindow, show=”*”,
 relief=FLAT, textvariable=self.passwordS)
 self.passwordE.place(x=70, y=120)
 self.submit = Button(self.registerWindow,
 text=”Submit”, pady=5, padx=20, command=self.add)
 self.submit.place(x=100, y=150)

# Actual Variales
 self.username = self.usernameS.get()
 self.password = self.passwordS.get()

self.salt = bcrypt.gensalt()
 self.hashed = bcrypt.hashpw(self.password.encode(), self.salt)

def run(self):
 self.registerWindow.mainloop()

def add(self):
 data = (self.username,)

result = db.searchData(data)

print(result)

if result != 0:
 data = (self.username, self.hashed)
 db.insertData(data)
 messagebox.showinfo(“Successful”, “Username Was Added”)
 else:
 messagebox.showwarning(“Warning”, “Username already Exists”)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Explaining Code
&lt;/h3&gt;

&lt;p&gt;First of all, We Import Libraries —&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bcrypt — for Encrypting Password

Tkinter — GUI library

database — It is our python file which has some SQlite Code in it
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Then, We create Instance of Database Class present inside database.py&lt;/p&gt;

&lt;p&gt;And Then, call the method of that class — Which will create a Table for us&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;db = Database()
db.createTable()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;We Defined Login Class, which will handle all Login related Stuff&lt;/p&gt;

&lt;p&gt;Then we define &lt;strong&gt;Dunder Method or Special Method of Python&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def __init__():
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;In that, we have some Tkinter related stuff.&lt;/p&gt;

&lt;p&gt;Second Method of Login Class:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def validate():
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This method will Validate Login and match the Password.&lt;/p&gt;

&lt;p&gt;It arranges username — for finding the user in the database in a tuple&lt;/p&gt;

&lt;p&gt;Then arranges input data in a tuple&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def validate(self):
 data = (self.username,)
 inputData = (self.username, self.password,)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After That, We check to call a method from Database Class to ValidateData or check Data.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;try:
 if (db.validateData(data, inputData)):
 messagebox.showinfo(“Successful”, “Login Was Successful”)
 else:
 messagebox.showerror(“Error”, “Wrong Credentials”)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you look carefully, We Started a Try — Except Block.&lt;/p&gt;

&lt;p&gt;We check if we have some return value and then show the user “They Have Successfully Logged In”&lt;/p&gt;

&lt;p&gt;Else we show the error&lt;/p&gt;

&lt;p&gt;Except Block —&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;except IndexError:
 messagebox.showerror(“Error”, “Wrong Credentials”)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If We Not found them in the Database, then SQLite will throw **IndexError, **which we can tackle easily...&lt;/p&gt;

&lt;p&gt;After that, we defined a Register Class — Which will do the same but it adds it the to database and with&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;self.salt = bcrypt.gensalt()
self.hashed = bcrypt.hashpw(self.password.encode(), self.salt)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;It is used to encrypt Password.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;database.py&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Importing Important Libraries

import sqlite3
import bcrypt

class Database:
 ‘’’
 Database Class for sqlite3
 :params conn — sqlite3Connection
 :params curr — cursor
 ‘’’

def __init__(self):
 try:
 self.conn = sqlite3.connect(“test.db”)
 print(“Successfully Opened Database”)
 self.curr = self.conn.cursor()
 except:
 print(“Failed”)

def createTable(self):
 ‘’’
 Method for Creating Table in Database
 ‘’’

create_table = ‘’’
 CREATE TABLE IF NOT EXISTS cred(
 id Integer PRIMARY KEY AUTOINCREMENT,
 username TEXT NOT NULL,
 password TEXT NOT NULL
 );
 ‘’’

self.curr.execute(create_table)
 self.conn.commit()

def insertData(self, data):
 ‘’’
 Method for Inserting Data in Table in Database
 ‘’’

insert_data = “””
 INSERT INTO cred(username, password)
 VALUES(?, ?);
 “””
 self.curr.execute(insert_data, data)
 self.conn.commit()

def searchData(self, data):
 ‘’’
 Method for Searching Data in Table in Database
 ‘’’

search_data = ‘’’
 SELECT * FROM cred WHERE username = (?);
 ‘’’

self.curr.execute(search_data, data)

rows = self.curr.fetchall()
 if rows == []:
 return 1
 return 0

def validateData(self, data, inputData):
 ‘’’
 Method for Validating Data Table in Database
 ‘’’

print(data)
 print(inputData)

validate_data = “””
 SELECT * FROM cred WHERE username = (?);
 “””

self.curr.execute(validate_data, data)
 row = self.curr.fetchall()

if row[0][1] == inputData[0]:
 return row[0][2] == bcrypt.hashpw(inputData[1].encode(), row[0][2])
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;These are some bunch of SQL Commands executed with Python.&lt;/p&gt;

&lt;p&gt;Thanks for Your Time.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;strong&gt;Stay Home, Stay Safe&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;Satyam Kulkarni&lt;/p&gt;

</description>
      <category>login</category>
      <category>python</category>
      <category>website</category>
    </item>
  </channel>
</rss>
