Advertisement
Guest User

Untitled

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