Guest User

Untitled

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