LouieFord

Untitled

Mar 27th, 2022
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. # Louie Ford Coursework Task 2
  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 Customer and a Date here : ", grid=[1,0])
  17.  
  18. # making a listbox so the user can chose from the variety of different genres
  19. listboxcustomer = ListBox(app, items=[], grid=[1,1], scrollbar=True)
  20.  
  21. # creating a function that fetches all customers from the table 'Customers'
  22. # DISTINCT will only pick unique names, so the same person doesn't appear twice
  23.  
  24. def populateCustomerList():
  25. myconnection = sqlite3.connect("chinook.db")
  26. mycursor = myconnection.cursor()
  27. mycursor.execute("""SELECT DISTINCT customers.FirstName FROM customers""")
  28.  
  29. allCustomers = mycursor.fetchall()
  30. for aCustomer in allCustomers:
  31. # [0] adds the first item in the tuple from each customer
  32. # in this case, it is the first name of the customer
  33. listboxcustomer.append(aCustomer[0])
  34. mycursor.close()
  35. myconnection.close()
  36.  
  37. # calling the function so the ListBox is filled with customer names
  38. populateCustomerList()
  39.  
  40. # commit function that will join invoice and customer tables
  41. # this is so we can get all of the invoices that are associated with the customer name
  42. def commit(listboxcustomer):
  43. mycursor.execute("""
  44. SELECT invoices.total
  45. FROM customers
  46. INNER JOIN invoices ON invoices.CustomerId = customers.CustomerId
  47. WHERE FirstName = ? """,
  48. (listboxcustomer.value,))
  49. # this list box will show all of the albums that are part of the chosen genre
  50. invoicelist = ListBox(app, items=([x[0] for x in mycursor.fetchall()]),grid=[0,3], scrollbar=True)
  51.  
  52.  
  53. # submit button so the user can confirm they want to chose that genre and then outputs all of the albums
  54. submit = PushButton(app, text="Generate Tracks!", command=commit, args=[listboxcustomer,],grid=[4,4])
  55.  
  56. # highlight and lowlight functions added to the pushbuttons to make it clear to the user which button they are about to press
  57. def highlight():
  58. quitApp.bg = "red"
  59. def lowlight():
  60. quitApp.bg = "grey"
  61.  
  62. def highlight2():
  63. submit.bg = "green"
  64. def lowlight2():
  65. submit.bg = "grey"
  66.  
  67. # pushbutton that closes the app when pressed
  68. quitApp = PushButton(app, command=quit, text="Quit App",grid=[0,5])
  69.  
  70. # using events to determine if the pushbutton is highlighted or not
  71. quitApp.when_mouse_enters = highlight
  72. quitApp.when_mouse_leaves = lowlight
  73. submit.when_mouse_enters = highlight2
  74. submit.when_mouse_leaves = lowlight2
  75.  
  76. # calling the display function so the app runs on screen for the user to see
  77. app.display()
Advertisement
Add Comment
Please, Sign In to add comment