· 8 years ago · Dec 13, 2017, 02:08 AM
1#---------------
2#-[Dictionary]-:
3#---------------
4
5# print()---Prints a given statement or variable
6
7# <Variable Name> = ---Sets a variable from text or another variable or both
8
9# if <Event does or does not do a specific event>:---This makes any indented line below if happen if the statement lines up
10
11# else:---This makes any indented line below happen if the if statement connected with it doesnt happen
12
13# import <module name>---imports specific modules to be used within python
14
15# while <Event is true>:---While loop something indented below it continues until the event becomes untrue
16
17# for <i> in <Variable>: for everything within a variable something involving it occurs
18
19# str(<Variable Name>)---converts variable to a string (word)
20
21# int(<Variable Name>)---Converts variable to an integer (number)
22
23# == --- does equal
24
25# != --- does not equal
26
27# / --- divide
28
29# x --- multiply
30
31# + --- addition
32
33# - --- Subtraction
34
35# < --- Less than
36
37# > --- Greater Than
38
39# <= ---Less than or equal to
40
41# >= ---Greater than or equal to
42
43# <variable> = len(String Variable)---sets variable as the length of another string variable
44
45# import time--- Imports time module to allow for time to pass
46
47# time.sleep(<seconds>)---Makes the system halt processing any further lines of code until after the time has elapsed
48
49# def <Function Name>():---anything indented within will only run when the function is called by the system
50
51
52
53#--------------
54#--[tkinter]--:
55#--------------
56
57# from tkinter import as * -----necessary module to allow for tkinter to be used
58
59# <Window Name> = Tk()---Creates A Window with the Variable name of whatever <Window Name> is
60
61# <Window Name>.title("<Whatever you want the Window Name on the window to be>")---Sets the Windows Title
62
63# <Window Name>.geometry("<X Value,Determines windows Length>x<Y Value, Determines Windows Height>")---Sets Size of specified window
64
65# <Window Name>.configure(background="#<Hexadecimal Code for wanted background colour>")---Sets Background Colour of specified window
66
67# <Window Name>.destroy()---Closes Window
68#---------------------------------------------
69#---Labels,Entries and Buttons for Tkinter---:
70#---------------------------------------------
71
72
73# <Label Name> = Label(<Window Name>,text=("<Desired Text For The Label>")).place(x=<x Co-ordinate>,y=<Y Co-Ordinate>)---Creates Label In A desired position with desired text on a desired window
74
75# <Button Name> = Button(<Window Name>,text=("<Desired Text For The Button>")).place(x=<X Co-ordinate>,y=<Y Co-Ordinate>)---Create Button in a desired position with desired text on a desired window
76
77# <Entry Name> = Entry(<Window Name>).place(x=<X Co-Ordinates>,y=<Y Co-Ordinates>)---Creates Entry in a Desired window in a desired location
78
79#--------------------------------------------------------------------------
80# -----These COULD Be used within Labels,Buttons and Entries Brackets-----:
81#--------------------------------------------------------------------------
82
83# ----- fg="#<Hexadecimal Value>" ---Changes The Default Foreground colour to a specified colour
84
85# ----- bg="#<Hexadecimal Value>" ---Changes The Default Background colour to a specified colour
86
87# ----- width=<desired Width of widget>---sets widget width
88
89# ----- height=<desired Height of widget>---sets widget height
90
91#---------------------------------------------------------------------
92# ----------This COULD be used within only buttons Brackets----------:
93#---------------------------------------------------------------------
94
95# ----------command=<Any valid Function>---Runs Any Valid Function given when button is clicked
96
97#---------------------------------------------------------------------
98# ---------------These COULD be used with only entries---------------:
99#---------------------------------------------------------------------
100
101# ---------------<Variable Name> = <Entry Name>.get()---retrieves data from entry and saves it as a variable
102# ---------------<Entry Name>.delete(0,'end')---deletes all contents within an entry
103
104
105
106#---------------
107# --[SQLite3]--:
108#---------------
109
110# from sqlite3 import *---Imports SQl Module
111
112#----------------
113# table creation:
114#----------------
115
116#def <Table Function Name>():
117# with connect("<Database Name>.db") as db:
118# cursor = db.cursor() ---Allows for Control with SQL and the database
119# cursor.execute(""" ---Executes a SQL recognised command
120# CREATE TABLE IF NOT EXISTS <Database Name>(
121# <IDnumber> integer,
122# <Column For Table, 1> text, ---Can Have any number of columns both as text or integers
123# <Column for Table, 2> integer,
124# Primary Key(<IDnumber>));""")
125# db.commit()
126
127#-----------------------------------------------------------
128# data submittion to applicable tables (Useful for signups):
129#-----------------------------------------------------------
130
131#def <Submit Data To Table Function Name>():
132# with connect("<Database Name>.db") as db:
133# cursor = db.cursor()
134# <Variable Name> = "insert into <Database Name> (<Column For Table,1>,<Column For Table,2>) values(?,?)"---Can include more columns,amount of columns are directlyproportional to theamount of question marks
135# cursor.execute(<Variable Name>, (<Variable storing data wanted in Column 1>,<Variable storing data wanted in Column 2>))---has to have same number of variable as the columns in the table that are being filled
136
137#----------------------------------------------
138# Checking data from table (Useful for Logins):
139#----------------------------------------------
140
141#def <Check Matching Data From Table Function Name>():
142# <Variable 1> = (x)---Variable must be assigned to compare to table values eg x
143# <Variable 2> = (y)---Variable must be assigned to compare to table values eg y
144# db = connect("<Database>.db")
145# cursor = db.cursor()
146# cursor.execute("SELECT * FROM <Database Name> WHERE <Column For Table,1> = ? AND <Column For Table,2> = ?",(<Variable 1>, <Variable 2>))
147# if cursor.fetchall():
148# <Any action required if the variables do match up is placed here>
149# else:
150# <If the Selection found no match then any action regarding that goes here>
151
152#---------------------------------------------------------------------------
153# Selecting for anything in a table with an in common value within a column:
154#---------------------------------------------------------------------------
155
156#def <Selection of in common values In a Table Function name>():
157# <Variable> = (x)---Variable must be assigned to find and compare column values that are the same eg x
158# db = connect("<Database Name>.db")
159# cursor = db.cursor()
160# cursor.execute("SELECT * FROM <Database Name> WHERE <Column Name with the in common value> = ?", (<Variable>,))
161# <variable With Values From table that have an incommon value> = cursor.fetchall()---This Value Can be used anywhere and be ouputted to show what is wanted may require conversion to string
162
163
164
165#------------------
166# --[.txt Files]--:
167#------------------
168
169#<Write To File Set Name> = open("<Name of text Document">.txt","w")----w opens the file to a write over state also create file if none there
170
171#<Write To File Set Name> = open("<Name of text Document">.txt","r")----r opens the file to a read state only can be done if file already made cannot be written to
172
173#<Write To File Set Name> = open("<Name of text Document">.txt","a")----a opens the file to an add to current content state adds extra to already made txt file
174
175#<Write To File Set Name>.close()---closes connection to txt file
176
177#<Write To File Set Name>.write("")---Enters Anything to document that is within brackets,whether variable or speech marked
178
179#<Write To File Set Name>.write("\n")---any text after this is on another line
180
181#<Write To File Set Name> = open("{0}.txt".format(<Variable to determine name of file>),'<can be in any form, r, w or a>')---Allows for variable based names
182
183###################################################################################################################################################################
184
185
186
187###################################################################################################################################################################
188#----------[OCRTUNES]----------#
189
190
191#--------------------
192# [Necessary Modules]
193#--------------------
194from tkinter import *
195from sqlite3 import *
196#---------------------
197# [/Necessary Modules]
198#---------------------
199
200#----------[Accounts Database]----------#
201
202def AccountsDatabase():
203#creates function to be utilised later all at once
204 with connect("Accounts.db") as db:
205#Connects to a database file
206 cursor = db.cursor()
207#Grants Program contol to utilise SQL processes
208 cursor.execute("""
209CREATE TABLE IF NOT EXISTS Accounts(
210AccID integer,
211Name text,
212Password text,
213DOB integer,
214FavArtist text,
215FavGenre text,
216Primary Key(AccID));""")
217#Creates a database if none made already and sets these as columns
218 db.commit()
219#saves database changes
220
221AccountsDatabase()
222
223#----------[/Accounts Database]---------#
224
225
226
227#----------[Sign Up Window]----------#
228
229def SignUpWindow():
230 SignUpWindow= Tk()
231 SignUpWindow.title("OCRTunes")
232 SignUpWindow.geometry("300x400")
233 SignUpWindow.configure(background="#ffffff")
234
235 OCRTunesSignUpLabel = Label(SignUpWindow,text="OCRTunes Signup",bg="#ffffff").pack()
236
237
238 NameEntry = Entry(SignUpWindow,bg="#f0f0f0")
239 NameEntry.place(x=150,y=50)
240 NameEntLabel = Label(SignUpWindow,text="Name",bg="#ffffff").place(x=30,y=50)
241
242 PasswordEntry = Entry(SignUpWindow,bg="#f0f0f0")
243 PasswordEntry.place(x=150,y=100)
244 PasswordEntLabel = Label(SignUpWindow,text="Password",bg="#ffffff").place(x=30,y=100)
245
246 ConfirmPasswordEntry = Entry(SignUpWindow,bg="#f0f0f0")
247 ConfirmPasswordEntry.place(x=150,y=150)
248 ConfirmPasswordEntLabel = Label(SignUpWindow,text="Confirm Password",bg="#ffffff").place(x=30,y=150)
249
250 DOBEntry = Entry(SignUpWindow,bg="#f0f0f0")
251 DOBEntry.place(x=150,y=200)
252 DOBEntLabel = Label(SignUpWindow,text="Date Of Birth",bg="#ffffff").place(x=30,y=200)
253
254 FavArtistEntry = Entry(SignUpWindow,bg="#f0f0f0")
255 FavArtistEntry.place(x=150,y=250)
256 FavArtistEntLabel = Label(SignUpWindow,text="Favourite Artist",bg="#ffffff").place(x=30,y=250)
257
258 FavGenreEntry = Entry(SignUpWindow,bg="#f0f0f0")
259 FavGenreEntry.place(x=150,y=300)
260 FavGenreEntLabel = Label(SignUpWindow,text="Favourite Genre",bg="#ffffff").place(x=30,y=300)
261
262 def ClearSignUp():
263 NameEntry.delete(0,'end')
264 PasswordEntry.delete(0,'end')
265 ConfirmPasswordEntry.delete(0,'end')
266 DOBEntry.delete(0,'end')
267 FavArtistEntry.delete(0,'end')
268 FavGenreEntry.delete(0,'end')
269
270 def CreateAccount():
271 NameSU = NameEntry.get()
272 PasswordSU = PasswordEntry.get()
273 ConfirmPasswordSU = ConfirmPasswordEntry.get()
274 DOBSU = DOBEntry.get()
275 FavArtistSU = FavArtistEntry.get()
276 FavGenreSU = FavGenreEntry.get()
277
278 db = connect("Accounts.db")
279 cursor=db.cursor()
280 cursor.execute("SELECT * FROM Accounts WHERE Name = ?",(NameSU,))
281
282 if cursor.fetchall():
283 print("NO")
284 elif PasswordSU != ConfirmPasswordSU:
285 Error1 = Tk()
286 Error1.title("ERROR")
287 Error1.geometry("130x70")
288 Error1Label = Label(Error1,text="Passwords dont match").pack()
289 Error1Button = Button(Error1,text="Ok",command=Error1.destroy).pack()
290 elif NameSU == "" or PasswordSU == "" or ConfirmPasswordSU == "" or DOBSU == "" or FavArtistSU == "" or FavGenreSU == "":
291 Error2 = Tk()
292 Error2.title("ERROR")
293 Error2.geometry("100x70")
294 Error2Label = Label(Error2,text="Not all entries filled").pack()
295 Error2Button = Button(Error2,text="Ok",command=Error2.destroy).pack()
296 else:
297 with connect("Accounts.db") as db:
298 cursor = db.cursor()
299 sql = "insert into Accounts (Name, Password, DOB, FavArtist, FavGenre) values(?,?,?,?,?)"
300 cursor.execute(sql, (NameSU, PasswordSU, DOBSU, FavArtistSU, FavGenreSU))
301 Success = Tk()
302 Success.title("Welcome")
303 Success.geometry("200x70")
304 SuccessLabel = Label(Success,text="Your Account has been created").pack()
305 SuccessButton = Button(Success,text="Ok",command=Success.destroy).pack()
306 ClearSignUp()
307
308
309
310 ExitSignUpButton = Button(SignUpWindow,text="Exit",bg="#f0f0f0",command=SignUpWindow.destroy).place(x=50,y=350)
311 ClearSignUpButton = Button(SignUpWindow,text="Clear",bg="#f0f0f0",command=ClearSignUp).place(x=130,y=350)
312 SubmitSignUpButton = Button(SignUpWindow,text="Submit",bg="#f0f0f0",command=CreateAccount).place(x=220,y=350)
313
314#----------[/Sign Up Window]----------#
315
316
317#----------[Log In Window]----------#
318
319def LogInWindow():
320 LogInWindow = Tk()
321 LogInWindow.title("OCRTunes Login")
322 LogInWindow.geometry("200x225")
323 LogInWindow.configure(background="#ffffff")
324
325 OCRTunesLoginLabel = Label(LogInWindow,text="OCRTunes Login",bg="#ffffff").pack()
326
327 NameEntry2 = Entry(LogInWindow,bg="#f0f0f0",width=17)
328 NameEntry2.place(x=75,y=50)
329 NameLabel2 = Label(LogInWindow,text="Name",bg="#ffffff").place(x=10,y=50)
330
331 PasswordEntry2 = Entry(LogInWindow,bg="#f0f0f0",width=17)
332 PasswordEntry2.place(x=75,y=100)
333 PasswordLabel2 = Label(LogInWindow,text="Password",bg="#ffffff").place(x=10,y=100)
334
335 def EnterProgram():
336 Welcome.destroy()
337 LogInWindow.destroy()
338 StartWindow.destroy()
339 MainWindow()
340
341
342 def SubmitLogIn():
343 NameLI = NameEntry2.get()
344 PasswordLI = PasswordEntry2.get()
345 db = connect("Accounts.db")
346 cursor = db.cursor()
347 cursor.execute("SELECT * FROM Accounts WHERE Name = ? AND Password = ?",(NameLI,PasswordLI))
348 if cursor.fetchall:
349 NameEntry2.delete(0,'end')
350 PasswordEntry2.delete(0,'end')
351 Welcome = Tk()
352 Welcome.title("Welcome")
353 Welcome.geometry("200x70")
354 WelcomeLabel = Label(Welcome,text="Welcome "+NameLI,).pack()
355
356 def EnterProgram():
357 Welcome.destroy()
358 LogInWindow.destroy()
359 StartWindow.destroy()
360 MainWindow()
361
362 WelcomeButton = Button(Welcome,text="Enter",command=EnterProgram).pack()
363
364 else:
365 Error3 = Tk()
366 Error3.title("Invalid Details")
367 Error3.geometry("200x70")
368 Error3Label = Label(Error3,text="Invalid Account Information").pack()
369 Error3Button = Button(Error3,text="Ok",command=Error3.destroy).pack()
370
371
372 ExitLogInButton = Button(LogInWindow,text="Exit",bg="#f0f0f0",command=LogInWindow.destroy).place(x=50,y=150)
373 SubmitLogInButton = Button(LogInWindow,text="Submit",bg="#f0f0f0",command=SubmitLogIn).place(x=110,y=150)
374
375#----------[/Log In Window]---------#
376
377
378
379#----------[Main Window]----------#
380
381def MainWindow():
382#Creates Function To Be utilised at another time
383 MainWindow = Tk()
384#Creates MainWindow Window
385 MainWindow.title("OCRTunes")
386#Sets Windows Title
387 MainWindow.geometry("700x500")
388#Sets Windows Size
389 MainWindow.configure(background="#ffffff")
390#Sets Windows background colour
391
392 OCRTunesLabel = Label(MainWindow,text="OCRTunes",bg="#ffffff").place(x=20,y=30)
393#Creates Label On Main Window
394 SearchArtistLabel = Label(MainWindow,text="Enter an Artist:",bg="#f0f0f0",height=2).place(x=362,y=22)
395#Creates Label On Main Window
396 SearchArtistBgLayerLabel = Label(MainWindow,text="",bg="#f0f0f0",width=30,height=2).place(x=445,y=22)
397#Creates Label On Main Window
398 SearchArtistEntry = Entry(MainWindow,bg="#ffffff")
399#Creates Entry Box on Main Window
400 SearchArtistEntry.place(x=450,y=30)
401#Placed Entry Boxes
402
403 def SaveToText():
404#Creates Function To be utilised at some point at once
405 SearchArtist = SearchArtistEntry.get()
406#retrieves data from the Entry Box
407 FileArtistVar = ("List of Songs By "+SearchArtist)
408#Creates Variable to be used in naming the file customly
409 db = connect("SongLibrary.db")
410#Connects To SongLibrary Database
411 cursor = db.cursor()
412#Allows For the Program to execute SQL and database commands
413 cursor.execute("SELECT * FROM SongLibrary WHERE Artist = ?",(SearchArtist,))
414#Selects All Entries in Song Library Where The ARtist is the same as the entry given
415 result = cursor.fetchall()
416#Sets Results fetched as a variable
417 if result != "[]":
418
419#Sets those results as a string value
420 myfile = open("{0}.txt".format(FileArtistVar),'w')
421#Creates or overwrites a writable file with a custom name dpendant on the artist inputted
422 myfile.write("Song Search Results For Artist:"+SearchArtist)
423#Writes A Line To the text File
424 myfile.write("\n")
425#Writes Everything Following On Next Line
426 myfile.write("")
427#Blank Line
428 myfile.write("\n")
429#Writes everything following on next line
430 for i in result:
431 myfile.write("Song: {}, Artist: {}, Genre: {}, Length: {},".format(i[1],i[2],i[3],i[4]))
432 myfile.write("\n")
433#Write the results fetched from the database selection
434 myfile.close
435#closes access to file
436 if result == "[]":
437#if the select operation retrieves nothing
438 WriteToFileError = Tk()
439#Creates Window
440 WriteToFileError.title("Error")
441#Sets Window Title
442 WriteToFileError.geometry("200x500")
443#Sets Window Size
444 WriteErrorLabel = Label(WriteToFileError,text="Are you Sure you are entering a valid artist name?").pack()
445#Places Lable onto Window
446 SearchArtistButton = Button(MainWindow,text="Submit",bg="#f0f0f0",command=SaveToText).place(x=600,y=27)
447#Places Button with The ability to Run "SaveToText" When clicked
448
449 frame = Frame(MainWindow)
450 frame.place(x=20,y=70)
451
452
453 ListboxTest = Listbox(frame,width=70,height=20,bg="#f0f0f0")
454 ListboxTest.pack(side="left",fill="y")
455
456 ListboxTest2 = Listbox(MainWindow,width=29,height=10,bg="#f0f0f0")
457 ListboxTest2.place(x=500,y=70)
458
459 scrollbar=Scrollbar(frame)
460 scrollbar.config(command=ListboxTest.yview)
461 scrollbar.pack(side="right",fill="y")
462
463 ListboxTest.config(yscrollcommand=scrollbar.set)
464
465 SongViewingLabelBG = Label(MainWindow,width=25,height=11,bg="#f0f0f0",relief = "groove").place(x=500,y=220)
466
467 db = connect("SongLibrary.db")
468 cursor = db.cursor()
469 cursor.execute("SELECT * FROM SongLibrary")
470 TotalSongs = cursor.fetchall()
471 for i in TotalSongs:
472 ListboxTest.insert(END,"{} {},{},{},{}".format(i[0],i[1],i[2],i[3],i[4]))
473
474 def ViewSongs(event):
475 ListboxTest2.delete(0,'end')
476 hello = ListboxTest.get(ListboxTest.curselection())
477 SelectedSpecificEntry = (hello[0])
478 db = connect("SongLibrary.db")
479 cursor = db.cursor()
480 cursor.execute("SELECT * FROM SongLibrary WHERE SongID = ?",(SelectedSpecificEntry,))
481 SongLength = cursor.fetchall()
482 for i in SongLength:
483 ListboxTest2.insert(END,"Song: {}".format(i[1]))
484 ListboxTest2.insert(END,"Artist: {}".format(i[2]))
485 ListboxTest2.insert(END,"Genre: {}".format(i[3]))
486 ListboxTest2.insert(END,"Length: {}".format(i[4]))
487
488 PlaySong = Button(MainWindow,text="Play Song")
489 PlaySong.place(x=515,y=230)
490 StopSong = Button(MainWindow,text="Stop Song")
491 StopSong.place(x=515,y=270)
492
493 def ClearQueue():
494 ListboxTest2.delete(0,'end')
495
496 ClearQueue = Button(MainWindow,text="Clear Queue",command=ClearQueue)
497 ClearQueue.place(x=512,y=310)
498
499
500
501
502
503 ListboxTest.bind('<<ListboxSelect>>', ViewSongs)
504
505
506
507
508
509#----------[/MainWindow]----------#
510
511
512
513#----------[Start Window]----------#
514
515StartWindow = Tk()
516#Creates Tkinter Window
517StartWindow.title("OCRTunes Startup Window")
518#Sets windows Title
519StartWindow.geometry("200x250")
520#Sets Window Size
521StartWindow.configure(background="#ffffff")
522#Sets windows background colour
523
524StartWindowLabel = Label(StartWindow,text="OCRTunes",bg="#ffffff").pack()
525#Inserts A Label Onto the window
526
527LogInButton = Button(StartWindow,text="Log In",bg="#f0f0f0",height=2,width=6,command=LogInWindow).place(x=72,y=60)
528#Inserts a button onto the window
529
530SignUpButton = Button(StartWindow,text="Sign Up",bg="#f0f0f0",height=2,width=6,command=SignUpWindow).place(x=72,y=120)
531#inserts a button onto the window
532
533StartWindowExitButton = Button(StartWindow,text="Exit",bg="#f0f0f0",command=StartWindow.destroy).place(x=82,y=180)
534#inserts a button onto the window
535
536#----------[/Start Window]---------#