Advertisement
Guest User

Untitled

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