· 6 years ago · Mar 07, 2019, 09:58 PM
1with sqlite3.connect("User.db") as db:
2 cursor = db.cursor()
3
4cursor.execute("""CREATE TABLE IF NOT EXISTS user (
5 userID INTEGER PRIMARY KEY,
6 username VARCHAR(20) NOT NULL,
7 password VARCHAR(20) NOT NULL
8 )""")
9
10
11def login(usernameLogin, passwordLogin):
12 while True:
13 username = usernameLogin.get()#Asks for username
14 password = passwordLogin.get()#Asks for password
15 with sqlite3.connect("User.db") as db:#Creates a connection to database
16 c = db.cursor()
17 find_user = ("SELECT * FROM user WHERE username = ? AND password = ?")#Validates inputs for account
18 c.execute(find_user,[(username),(password)])
19 results = c.fetchall()#Fetches values from database
20
21 if results:#Validates if the username/password is recognised
22 for i in results:
23 messagebox.showinfo("", "Welcome "+i[1]+"!")
24 break
25
26 else:
27 messagebox.showinfo("", "Password and username is not recognised")
28 break
29
30def newUser(username1, password1):
31 found = 0
32 while found == 0:
33 username = username1.get()
34 with sqlite3.connect("User.db") as db:
35 c = db.cursor()
36 findUser = ("SELECT * FROM user WHERE username = ?")
37 c.execute(findUser, [(username)])#Checks existence of username in database
38
39 if c.fetchall():
40 messagebox.showinfo("Username", "Username taken please try again.")
41 break
42 else:
43 messagebox.showinfo("", "Account has been created!")
44 found = 1
45
46 password = password1.get()
47 insertData = '''INSERT INTO user(username, password)
48 VALUES(?,?)'''#Inserts new account into databse
49 c.execute(insertData, [(username),(password)])
50 db.commit()
51
52def newUserTkinter():
53 window = tkinter.Tk()
54 window.title("Create new account")
55
56 labelOne = ttk.Label(window, text = "Enter a username:")
57 labelOne.grid(row = 0, column = 0)
58 username1 = tkinter.StringVar(window)#value type is classified as a string
59 usernameEntry = ttk.Entry(window, width = 30, textvariable = username1)
60 usernameEntry.grid(row = 1, column = 0)
61
62 labelTwo = ttk.Label(window, text = "Enter a password:")
63 labelTwo.grid(row = 2, column = 0)
64 password1 = tkinter.StringVar(window)#value type is classified as a string
65 passwordEntry = ttk.Entry(window, width = 30, textvariable = password1)
66 passwordEntry.grid(row = 3, column = 0)
67
68 btn = ttk.Button(window, text="Submit", command=lambda: newUser(username1, password1))
69 btn.grid(row = 3, column = 1)
70
71def removeUser(usernameD, passwordD):
72 exists = 0
73 while exists == 0:#Validates exsistence of account username
74 username = usernameD.get()
75 password = passwordD.get()
76 with sqlite3.connect("User.db") as db:
77 c = db.cursor()
78 findUser = ("SELECT * FROM user WHERE username = ?")
79 c.execute(findUser, [(username)])
80
81 if c.fetchall():
82 messagebox.showinfo("Delete account", "Account deleted!")
83 exists = 1
84 else:
85 messagebox.showinfo("", "Account does not exist")
86 break
87
88 remove_user = ("DELETE from user WHERE username = ? AND password = ?")
89 c.execute(remove_user,[(username),(password)])
90 db.commit()
91
92def removeUserTkinter():
93 window = tkinter.Tk()
94 window.title("Delete account")
95
96 labelOne = ttk.Label(window, text = "Enter account username:")
97 labelOne.grid(row = 0, column = 0)
98 usernameD = tkinter.StringVar(window)#value type is classified as a string
99 usernameEntry = ttk.Entry(window, width = 30, textvariable = usernameD)
100 usernameEntry.grid(row = 1, column = 0)
101
102 labelTwo = ttk.Label(window, text = "Enter account password:")
103 labelTwo.grid(row = 2, column = 0)
104 passwordD = tkinter.StringVar(window)#value type is classified as a string
105 passwordEntry = ttk.Entry(window, width = 30, textvariable = passwordD)
106 passwordEntry.grid(row = 3, column = 0)
107
108 btn = ttk.Button(window, text="Submit", command=lambda: removeUser(usernameD, passwordD))
109 btn.grid(row = 3, column = 1)
110
111def menu():
112 with sqlite3.connect("User.db") as db:
113 c = db.cursor()
114 c.execute("SELECT * FROM user")
115 print(c.fetchall())
116
117 window = tkinter.Tk()
118 window.title("Treasure Hunt Game!")
119
120 labelOne = ttk.Label(window, text = """ ~~~~~~~~~~~~~ USER MENU ~~~~~~~~~~~~~
121 """)#label displays instruction
122 labelOne.grid(row = 0, column = 0)#places label in a grid
123
124 btn = ttk.Button(window, text = "Create account", command = newUserTkinter)
125 btn.grid(row = 1, column = 0)#places button in a grid
126
127 btn = ttk.Button(window, text = "Delete account", command = removeUserTkinter)
128 btn.grid(row = 2, column = 0)#places button in a grid
129
130 labelTwo = ttk.Label(window, text = "Login to your account:")
131 labelTwo.grid(row = 3, column = 0)
132
133 usernameLogin = tkinter.StringVar(window)#value type is classified as a string
134 usernameEntry = ttk.Entry(window, width = 30, textvariable = usernameLogin)
135 usernameEntry.grid(row = 5, column = 0)
136
137 labelTwo = ttk.Label(window, text = "Username")
138 labelTwo.grid(row = 4, column = 0)
139
140 passwordLogin = tkinter.StringVar(window)#value type is classified as a string
141 passwordEntry = ttk.Entry(window, width = 30, textvariable = passwordLogin)
142 passwordEntry.grid(row = 7, column = 0)
143
144 labelTwo = ttk.Label(window, text = "Password")
145 labelTwo.grid(row = 6, column = 0)
146
147 btn = ttk.Button(window, text="Submit", command=lambda: login(usernameLogin, passwordLogin))
148 btn.grid(row = 7, column = 1)
149
150menu()