Advertisement
Guest User

working

a guest
Dec 9th, 2018
550
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.57 KB | None | 0 0
  1. import tkinter as tk #importd tkinter and renames it as tk
  2. import sqlite3
  3. import re
  4. conn = sqlite3.connect('OS_Employee.db')
  5. LARGE_FONT= ("Verdana", 12)
  6.  
  7.  
  8. class Gui(tk.Tk): #creat Gui class and inherit attributes of tk.TK
  9.  
  10. def __init__(self, *args, **kwargs): # the init methodology is going to initialize with the class right away this allows us to pass arguments and keyword arguments
  11.  
  12. tk.Tk.__init__(self, *args, **kwargs) #initialize tk within the initialization function
  13. container = tk.Frame(self) # defines the container to be filled with a bunch of frames to be used later
  14. container.pack(side="top", fill="both", expand = True) # configure general display
  15. container.grid_rowconfigure(0, weight=1)
  16. container.grid_columnconfigure(0, weight=1)
  17.  
  18. self.frames = {} #pre defined dictionary
  19. for F in (HomePage, LoginPage, RegistrationPage, Deliverable1, Display):
  20. frame = F(container, self)
  21. self.frames[F] = frame
  22. frame.grid(row=0, column=0, sticky="nsew") #using grid to place the widget
  23. self.show_frame(HomePage) #call show_frame to bring the frame we want
  24.  
  25. def show_frame(self, cont):
  26.  
  27. frame = self.frames[cont]
  28. frame.tkraise() # this will bring our frame to the top for the user to see
  29.  
  30.  
  31. class HomePage(tk.Frame):
  32.  
  33. def __init__(self, parent,controller): #homepage
  34. tk.Frame.__init__(self,parent)
  35. self.configure(background="light grey")
  36. label = tk.Label(self, text="Buisness Solution", font=LARGE_FONT, bg="light grey").pack()
  37. label1 = tk.Label(self, text="online office supply retailer that ships furniture, tech, pens,\n files, etc. to consumers and corperations nation wide.", font=LARGE_FONT, bg="light grey",bd=1,relief="solid")
  38. label1.pack(pady=10,padx=10)
  39. button = tk.Button(self, text="Login", bg="#70ad47", fg="white",
  40. command=lambda: controller.show_frame(LoginPage))
  41. button.pack()
  42.  
  43. button2 = tk.Button(self, text="New User?", bg="#70ad47", fg="white",
  44. command=lambda: controller.show_frame(RegistrationPage))
  45. button2.pack(pady=10)
  46.  
  47.  
  48. class LoginPage(tk.Frame):
  49.  
  50. def __init__(self, parent, controller): #LoginPage
  51. tk.Frame.__init__(self, parent)
  52. self.controller = controller
  53. self.configure(background="light grey")
  54. LPlabel0 = tk.Label(self, text="Enter Email and Password", font=LARGE_FONT, bg="light grey").grid(row=0,column=0)
  55. LPlabel1 = tk.Label(self, text="Email:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=1,column=0)
  56. LPlabel2 = tk.Label(self, text="Password:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=2,column=0)
  57. self.username = tk.Entry(self)
  58. self.username.grid(row=1,column=1)
  59. self.password = tk.Entry(self)
  60. self.password.grid(row=2,column=1)
  61. self.status = tk.Label(self, fg='red', bg="light grey")
  62. self.status.grid(row=0,column=1)
  63. button1 = tk.Button(self, text="Back", bg="dark gray", fg="white",
  64. command=self.back)
  65. button1.grid(sticky="W",row=3,column=1,pady=4)
  66.  
  67. button2 = tk.Button(self, text="Continue", bg="#70ad47", fg="white", #continue should go to diliverables
  68. command=self.login) #command=lambda: controller.show_frame(Deliverable1))
  69. button2.grid(sticky="E",row=3,column=1)
  70.  
  71. def back(self):
  72. self.username.delete(0, 'end')
  73. self.password.delete(0, 'end')
  74. self.status.config(text="")
  75. self.controller.show_frame(HomePage)
  76. def login(self):
  77. with conn:
  78. cur = conn.cursor()
  79. try:
  80. # loginTest = False # main condition to loop if email and password not met
  81. # while not loginTest: # wrong email loopy
  82. userEmail = self.username.get()
  83. userEmail = userEmail.lower().replace(" ", "")
  84. userPassword = self.password.get()
  85. print("Username: {}, password: {}".format(userEmail, userPassword))
  86. cur.execute(
  87. "SELECT COUNT (*) FROM Employee WHERE(Email= '" + userEmail.lower() + "' AND Password= '" + userPassword + "')")
  88. results = cur.fetchone() # return very first thing it finds that matches
  89. print(results[0]) # print first thing
  90. if results[0] == 1:
  91. print("Login successful")
  92. loginTest = True
  93. self.username.delete(0, 'end')
  94. self.password.delete(0, 'end')
  95. self.status.config(text="")
  96. self.controller.show_frame(Deliverable1)
  97. else:
  98. print("Login Unsuccessful")
  99. self.status.config(text="wrong password or user name")
  100. self.username.delete(0, 'end')
  101. self.password.delete(0, 'end')
  102. self.controller.show_frame(LoginPage)
  103.  
  104. except Exception as e:
  105. print(e)
  106. self.status.config(text="connection failed")
  107.  
  108.  
  109. class RegistrationPage(tk.Frame): #registrationpage
  110.  
  111. def __init__(self, parent, controller):
  112. self.controller = controller
  113. tk.Frame.__init__(self, parent)
  114. self.configure(background="light grey")
  115. label = tk.Label(self, text="Complete the fields below", font=LARGE_FONT, bg="light grey").grid(row=0,column=0)
  116. label1 = tk.Label(self, text="First Name:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=1,column=0)
  117. label11 = tk.Label(self, text="Last Name:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=2,column=0)
  118. label2 = tk.Label(self, text="Employee ID:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=3,column=0)
  119. label3 = tk.Label(self, text="E-mail:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=4,column=0)
  120. label5 = tk.Label(self, text="Password:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=5,column=0)
  121. # label6 = tk.Label(self, text="Confirm PW:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=6,column=0)
  122. self.firstname = tk.Entry(self)
  123. self.firstname.grid(row=1,column=1)
  124. self.lastname = tk.Entry(self)
  125. self.lastname.grid(row=2,column=1)
  126. self.id = tk.Entry(self)
  127. self.id.grid(row=3,column=1)
  128. self.email = tk.Entry(self)
  129. self.email.grid(row=4,column=1)
  130. self.password = tk.Entry(self)
  131. self.password.grid(row=5,column=1)
  132. self.status = tk.Label(self, fg='red', bg="light grey")
  133. self.status.grid(row=0,column=1)
  134. # self.confirm_pass = tk.Entry(self)
  135. # self.confirm_pass.grid(row=6,column=1)
  136.  
  137. button1 = tk.Button(self, text="Back", bg="dark gray", fg="white",
  138. command=self.back)
  139. button1.grid(sticky="w",row=6,column=1,pady=4)
  140.  
  141. button2 = tk.Button(self, text="Continue", bg="#70ad47", fg="white",
  142. command=self.register) # command=lambda: controller.show_frame(LoginPage))
  143. button2.grid(sticky="E",row=6,column=1)
  144.  
  145. def back(self):
  146. self.firstname.delete(0, "end")
  147. self.lastname.delete(0, "end")
  148. self.password.delete(0, "end")
  149. self.email.delete(0, "end")
  150. self.id.delete(0, "end")
  151. self.status.config(text="")
  152. self.controller.show_frame(HomePage)
  153. def register(self):
  154. with conn:
  155. cur = conn.cursor()
  156. try:
  157. if self.id.get():
  158. EmployeeID = int(self.id.get())
  159.  
  160. FirstName = self.firstname.get()
  161. # while FirstName == "":
  162. # FirstName = self.firstname.get()
  163. # FirstName = FirstName.title()
  164.  
  165. LastName = self.lastname.get()
  166. # while LastName == "":
  167. # LastName = self.lastname.get()
  168. # LastName = LastName.title()
  169.  
  170. Email = self.email.get().lower()
  171. match = re.match('^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$', Email)
  172. if match is None:
  173. print('Bad Syntax')
  174. #raise ValueError('Bad Syntax')
  175.  
  176. Password = self.password.get()
  177. Password = Password.lower()
  178.  
  179. cur.execute(
  180. 'insert into employee values(?,?,?,?,?)',
  181. (EmployeeID, FirstName, LastName, Email, Password))
  182. cur.execute(
  183. 'select * from employee where EmployeeId=?',
  184. (EmployeeID,))
  185.  
  186. results = cur.fetchall()
  187. print(results)
  188. self.firstname.delete(0, "end")
  189. self.lastname.delete(0, "end")
  190. self.password.delete(0, "end")
  191. self.email.delete(0, "end")
  192. self.id.delete(0, "end")
  193. self.status.config(text="")
  194. self.controller.show_frame(Deliverable1)
  195. except Exception as e:
  196. print(e)
  197. print("User already exist")
  198. self.status.config(text="incorrect information")
  199.  
  200. class Deliverable1(tk.Frame): #registrationpage
  201.  
  202. def __init__(self, parent, controller):
  203. tk.Frame.__init__(self, parent)
  204. self.controller = controller
  205. self.configure(background="light grey")
  206. label = tk.Label(self, text="Select what to display", font=LARGE_FONT, bg="light grey").grid(row=0,column=0)
  207. label1 = tk.Label(self, text="10 Most Profitable:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=1,column=0)
  208. label2 = tk.Label(self, text="10 least profitable:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=2,column=0)
  209. label3 = tk.Label(self, text="Which State:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=3,column=0)
  210. label5 = tk.Label(self, text="Which Product:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=4,column=0)
  211. label6 = tk.Label(self, text="Year:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=5,column=0)
  212. label7 = tk.Label(self, text="Month:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=6,column=0)
  213. label8 = tk.Label(self, text="Quarter:", font=LARGE_FONT, bg="light grey").grid(sticky="E",row=7,column=0)
  214. entry1 = tk.Entry(self).grid(sticky="W",row=6,column=1)
  215. entry2 = tk.Entry(self).grid(sticky="W",row=7,column=1)
  216. self.status = tk.Label(self, fg='red', bg="light grey")
  217. self.status.grid(row=0,column=1)
  218.  
  219. v = tk.IntVar()
  220. tk.Radiobutton(self, variable=v, value=1, bg="light grey").grid(sticky="W",row=2,column=1)
  221. tk.Radiobutton(self, variable=v, value=2, bg="light grey").grid(sticky="W",row=1,column=1)
  222.  
  223. button1 = tk.Button(self, text="Log Out", bg="red", fg="white", #make the back button a logout button?
  224. command=self.logout) #command=lambda: controller.show_frame(HomePage))
  225. button1.grid(sticky="w",row=8,column=1,pady=5)
  226.  
  227. button2 = tk.Button(self, text="Continue", bg="#70ad47", fg="white", #go to displayproducts in order
  228. command=lambda: controller.show_frame(Display))
  229. button2.grid(sticky="E",row=8,column=1)
  230.  
  231. self.state = tk.Entry(self)
  232. self.state.grid(sticky="W",row=3,column=1)
  233.  
  234. self.product = tk.Entry(self)
  235. self.product.grid(sticky="W",row=4,column=1)
  236.  
  237. self.year = tk.Entry(self)
  238. self.year.grid(sticky="W",row=5,column=1)
  239.  
  240. def logout(self):
  241. self.status.config(text="")
  242. self.controller.show_frame(HomePage)
  243.  
  244.  
  245.  
  246.  
  247.  
  248. class Display(tk.Frame):
  249.  
  250. def __init__(self, parent, controller): #homepage
  251. tk.Frame.__init__(self,parent)
  252. self.configure(background="light grey")
  253. label = tk.Label(self, text="Profitable products", font=LARGE_FONT, bg="light grey").pack(pady=10)
  254.  
  255. listbox = tk.Listbox(self).pack()
  256.  
  257. #for i in range(20):
  258. #listbox.insert(END, str(i))
  259.  
  260. button = tk.Button(self, text="Back", bg="dark grey", fg="white",
  261. command=lambda: controller.show_frame(Deliverable1))
  262. button.pack(pady=7)
  263.  
  264.  
  265. app = Gui() #App is the object of the Gui class
  266. app.mainloop() # then run .mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement