Advertisement
Guest User

Untitled

a guest
Nov 5th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.21 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox as ms
  3. import sqlite3
  4. import sys
  5.  
  6.  
  7. with sqlite3.connect('my.db') as db:
  8.     c = db.cursor()
  9.  
  10. c.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL PRIMARY KEY, password TEXT NOT NULL);')
  11. db.commit()
  12. db.close()
  13.  
  14.  
  15.  
  16. class Main:
  17.  
  18.     counter = 0
  19.     def __init__(self, master):
  20.         # Window
  21.         self.master = master
  22.         # Some Usefull variables
  23.         self.username = tk.StringVar()
  24.         self.password = tk.StringVar()
  25.         self.n_username = tk.StringVar()
  26.         self.n_password = tk.StringVar()
  27.         # Create Widgets
  28.         self.widgets()
  29.  
  30.  
  31.     # Login Function
  32.     def login(self):
  33.         # Establish Connection
  34.         with sqlite3.connect('my.db') as db:
  35.             c = db.cursor()
  36.  
  37.         # Find user If there is any take proper action
  38.         find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
  39.         c.execute(find_user, [(self.username.get()), (self.password.get())])
  40.         result = c.fetchall()
  41.         if result:
  42.             self.logf.pack_forget()
  43.             self.head['text'] = self.username.get() + '\n Loged In'
  44.             self.head['pady'] = 150
  45.         else:
  46.             ms.showerror('Oops!', 'Username Not Found.')
  47.  
  48.     def new_user(self):
  49.         # Establish Connection
  50.         with sqlite3.connect('my.db') as db:
  51.             c = db.cursor()
  52.  
  53.         # Find Existing username if any take proper action
  54.         find_user = ('SELECT username FROM user WHERE username = ?')
  55.         c.execute(find_user, [(self.n_username.get())])
  56.         if c.fetchall():
  57.             ms.showerror('Error!', 'Username Taken Try a Diffrent One.')
  58.         else:
  59.             ms.showinfo('Success!', 'Account Created!')
  60.             self.log()
  61.         # Create New Account
  62.         insert = 'INSERT INTO user(username,password) VALUES(?,?)'
  63.         c.execute(insert, [(self.n_username.get()), (self.n_password.get())])
  64.         db.commit()
  65.  
  66.  
  67.  
  68.     def log(self):
  69.         self.username.set('')
  70.         self.password.set('')
  71.         self.crf.pack_forget()
  72.         self.head['text'] = 'LOGIN'
  73.         self.logf.pack()
  74.  
  75.     def cr(self):
  76.         self.n_username.set('')
  77.         self.n_password.set('')
  78.         self.logf.pack_forget()
  79.         self.head['text'] = 'Create Account'
  80.         self.crf.pack()
  81.  
  82.     # Draw Widgets
  83.     def widgets(self):
  84.         self.head = tk.Label(self.master, text='LOGIN', font=('', 35), pady=10)
  85.         self.head.pack()
  86.         self.logf = tk.Frame(self.master, padx=10, pady=10)
  87.         tk.Label(self.logf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky='W')
  88.         tk.Entry(self.logf, textvariable=self.username, bd=5, font=('', 15)).grid(row=0, column=1)
  89.         tk.Label(self.logf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky='W')
  90.         tk.Entry(self.logf, textvariable=self.password, bd=5, font=('', 15), show='*').grid(row=1, column=1)
  91.         tk.Button(self.logf, text=' Login ', bd=3, font=('', 15), padx=5, pady=5, command=self.login).grid()
  92.         tk.Button(self.logf, text=' Create Account ', bd=3, font=('', 15), padx=5, pady=5, command=self.cr).grid(row=2,
  93.                                                                                                               column=1)
  94.         self.logf.pack()
  95.  
  96.         self.crf = tk.Frame(self.master, padx=10, pady=10)
  97.         tk.Label(self.crf, text='Username: ', font=('', 20), pady=5, padx=5).grid(sticky='W')
  98.         tk.Entry(self.crf, textvariable=self.n_username, bd=5, font=('', 15)).grid(row=0, column=1)
  99.         tk.Label(self.crf, text='Password: ', font=('', 20), pady=5, padx=5).grid(sticky='W')
  100.         tk.Entry(self.crf, textvariable=self.n_password, bd=5, font=('', 15), show='*').grid(row=1, column=1)
  101.         tk.Button(self.crf, text='Create Account', bd=3, font=('', 15), padx=5, pady=5, command=self.new_user).grid()
  102.         tk.Button(self.crf, text='Go to Login', bd=3, font=('', 15), padx=5, pady=5, command=self.log).grid(row=2,
  103.                                                                                                          column=1)
  104.  
  105.  
  106. if __name__ == '__main__':
  107.     root = tk.Tk()
  108.     root.title('Login Form')
  109.     Main(root)
  110.     root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement