Guest User

Untitled

a guest
Mar 26th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.48 KB | None | 0 0
  1. #imports
  2. from tkinter import *
  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
  7. with sqlite3.connect('quit.db') as db:
  8. c = db.cursor()
  9.  
  10. c.execute('CREATE TABLE IF NOT EXISTS user (username TEXT NOT NULL ,password TEX NOT NULL);')
  11. db.commit()
  12. db.close()
  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. def NewPage():
  28. global NewRoot
  29. root.withdraw() # hide (close) the root/Tk window
  30. NewRoot = tk.Toplevel(root)
  31. # use the NewRoot as the root now
  32.  
  33. #Login Function
  34. def login(self):
  35. with sqlite3.connect('quit.db') as db:
  36. c = db.cursor()
  37.  
  38. #Find user If there is any take proper action
  39. find_user = ('SELECT * FROM user WHERE username = ? and password = ?')
  40. c.execute(find_user,[(self.username.get()),(self.password.get())])
  41. result = c.fetchall()
  42. if result:
  43. self.logf.pack_forget()
  44. self.head['text'] = self.username.get() + 'n Logged In'
  45. self.head['pady'] = 150
  46. root.after(2000, NewPage)
  47. else:
  48. ms.showerror('Oops!','Username Not Found.')
  49.  
  50. def new_user(self):
  51. #Establish Connection
  52. with sqlite3.connect('quit.db') as db:
  53. c = db.cursor()
  54.  
  55. #Find Existing username if any take proper action
  56. find_user = ('SELECT * FROM user WHERE username = ?')
  57. c.execute(find_user,[(self.username.get())])
  58. if c.fetchall():
  59. ms.showerror('Error!','Username Taken Try a Diffrent One.')
  60. else:
  61. ms.showinfo('Success!','Account Created!')
  62. self.log()
  63. #Create New Account
  64. insert = 'INSERT INTO user(username,password) VALUES(?,?)'
  65. c.execute(insert,[(self.n_username.get()),(self.n_password.get())])
  66. db.commit()
  67.  
  68. #Frame Packing Methords
  69. def log(self):
  70. self.username.set('')
  71. self.password.set('')
  72. self.crf.pack_forget()
  73. self.head['text'] = 'LOGIN'
  74. self.logf.pack()
  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 = Label(self.master,text = 'LOGIN',font = ('',35),pady = 10)
  85. self.head.pack()
  86. self.logf = Frame(self.master,padx =10,pady = 10)
  87. Label(self.logf,text = 'Username: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
  88. Entry(self.logf,textvariable = self.username,bd = 5,font = ('',15)).grid(row=0,column=1)
  89. Label(self.logf,text = 'Password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
  90. Entry(self.logf,textvariable = self.password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
  91. Button(self.logf,text = ' Login ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.login).grid()
  92. Button(self.logf,text = ' Create Account ',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.cr).grid(row=2,column=1)
  93. self.logf.pack()
  94.  
  95. self.crf = Frame(self.master,padx =10,pady = 10)
  96. Label(self.crf,text = 'Username: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
  97. Entry(self.crf,textvariable = self.n_username,bd = 5,font = ('',15)).grid(row=0,column=1)
  98. Label(self.crf,text = 'Password: ',font = ('',20),pady=5,padx=5).grid(sticky = W)
  99. Entry(self.crf,textvariable = self.n_password,bd = 5,font = ('',15),show = '*').grid(row=1,column=1)
  100. Button(self.crf,text = 'Create Account',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.new_user).grid()
  101. Button(self.crf,text = 'Go to Login',bd = 3 ,font = ('',15),padx=5,pady=5,command=self.log).grid(row=2,column=1)
  102.  
  103.  
  104.  
  105. if __name__ == '__main__':
  106. #Create Object
  107. #and setup window
  108. root = Tk()
  109. root.title('Login Form')
  110. #root.geometry('400x350+300+300')
  111.  
  112. main(root)
  113. root.mainloop()
Add Comment
Please, Sign In to add comment