LouieFord

Untitled

Mar 27th, 2022
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. # Louie Ford Coursework Task 1
  2.  
  3. # Importing everything needed to make the program
  4. import sqlite3
  5. from guizero import App, PushButton, ListBox, TextBox, Text
  6. from typing import List
  7.  
  8. # creating a connection with the database and a cursor
  9. myconnection = sqlite3.connect("chinook.db")
  10. mycursor = myconnection.cursor()
  11.  
  12. # making an app for the program to go onto
  13. app = App(title="Album Searcher",layout="grid", bg="grey")
  14.  
  15. # Adding text to the top of the page giving instructions to the user
  16. text = Text(app, "Select a Genre of song here: ", grid=[1,0])
  17.  
  18. # making a listbox so the user can chose from the variety of different genres
  19. listboxgenre = ListBox(app, items=[], grid=[1,1], scrollbar=True)
  20.  
  21. # this funciton will connect to the database and fetch all of the genres in the genre table and display them into the ListBox
  22. def populateGenreList():
  23. myconnection = sqlite3.connect("chinook.db")
  24. mycursor = myconnection.cursor()
  25. mycursor.execute("Select genres.Name from genres order by genres.Name ASC")
  26.  
  27. allGenre = mycursor.fetchall()
  28. for aGenre in allGenre:
  29. # adding the first value from the tuple into the ListBox
  30. # in this case, it is the genre name
  31. listboxgenre.append(aGenre[0])
  32. mycursor.close()
  33. myconnection.close()
  34.  
  35. # calling the function so the ListBox is filled and user can select an option
  36. populateGenreList()
  37.  
  38. # this function is using an sql query to link the 'genres' and 'albums' tables from the database
  39. def commit(genrename):
  40. mycursor.execute("""
  41. SELECT DISTINCT albums.Title, genres.Name
  42. FROM albums
  43. INNER JOIN tracks ON tracks.AlbumId = albums.AlbumId
  44. INNER JOIN genres ON genres.GenreId = tracks.GenreId
  45. WHERE genres.Name = ? """,
  46. (genrename.value,))
  47. # this list box will show all of the albums that are part of the chosen genre
  48. albumlist = ListBox(app, items=([x[0] for x in mycursor.fetchall()]),grid=[0,3], scrollbar=True)
  49.  
  50. # submit button so the user can confirm they want to chose that genre and then outputs all of the albums
  51. submit = PushButton(app, text="Generate Tracks!", command=commit, args=[listboxgenre],grid=[5,5])
  52.  
  53. # highlight and lowlight functions added to the pushbuttons to make it clear to the user which button they are about to press
  54. def highlight():
  55. quitApp.bg = "red"
  56. def lowlight():
  57. quitApp.bg = "grey"
  58.  
  59. def highlight2():
  60. submit.bg = "green"
  61. def lowlight2():
  62. submit.bg = "grey"
  63.  
  64. # pushbutton that closes the app when pressed
  65. quitApp = PushButton(app, command=quit, text="Quit App",grid=[0,5])
  66.  
  67. # using events to determine if the pushbutton is highlighted or not
  68. quitApp.when_mouse_enters = highlight
  69. quitApp.when_mouse_leaves = lowlight
  70. submit.when_mouse_enters = highlight2
  71. submit.when_mouse_leaves = lowlight2
  72.  
  73.  
  74. #display the app on the screen for the user to use
  75. app.display()
Advertisement
Add Comment
Please, Sign In to add comment