Im working on my final project for my CS50P and i decided to make an game, GUESS THE SONG, using spotify api. User should on start page insert a link of public playlist(that part isnt done yet), and game should show track cover image and name of the song, and user should guess the artist. Program choose 5 random songs from playlist and it should display it one by one after guessing (app show cover and name of the song, user try to guess, program goes to the next song until it reach the end). In class GamePage, method playing, idk how to create my while part, how to wait for users input and then switch to another song
from dotenv import load_dotenv
import os
import base64
import tkinter as tk
from tkinter import ttk
import requests
import json
import re
import spotipy
import customtkinter as ctk
from PIL import Image
import io
import random
class SelectBox:
def __init__(self, parent, width=50, height=10, list_items=[], padx=0, pady=0, side="top"):
self.width = width
self.height = height
self.list_items = list_items
self.padx = padx
self.pady = pady
self.side = side
self.frm = ctk.CTkFrame(master=parent)
self.frm.pack(padx=self.padx, pady=self.pady, side=self.side)
self.string_var = tk.StringVar()
self.entry = ctk.CTkEntry(self.frm, textvariable=self.string_var, width=self.width)
self.entry.pack(fill="x", expand=True)
self.listbox_frame = ctk.CTkFrame(self.frm, width=self.width, height=self.height)
self.listbox_frame.pack()
self.list_box = tk.Listbox(self.listbox_frame, width=self.width, height=self.height)
self.list_box.pack()
self.list_box.bind("<MouseWheel>", lambda event: self.list_box.yview_scroll(int(event.delta / 60), "units"))
self.list_box.bind("<<ListboxSelect>>", self.on_select)
for item in self.list_items:
self.list_box.insert("end", item)
def on_select(self, event):
selected_index = self.list_box.curselection()
if selected_index:
selected_item = self.list_box.get(selected_index)
self.string_var.set(selected_item)
class SongEntry:
def __init__(self, parent, stringvar):
self.frame = ctk.CTkFrame(parent)
self.entry_song = ctk.CTkEntry(self.frame, width=300, height=40, textvariable=stringvar)
self.entry_song.pack(side="left")
self.next_button = ctk.CTkButton(self.frame, text="NEXT", height=40, command=lambda: stringvar.set(self.entry_song.get()))
self.next_button.pack(side="left")
def pack(self):
self.frame.pack(pady=20)
def pack_forget(self):
self.frame.pack_forget()
class CustomTkinterApp(ctk.CTk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
container = ctk.CTkFrame(self)
container.pack(side="top", fill="both", expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (StartPage, GamePage, EndPage):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(ctk.CTkFrame):
def __init__(self, parent, controller):
super().__init__(parent)
title = ctk.CTkLabel(self, text="GUESS THE SONG", font=("Calibri", 48))
title.pack(pady=50)
button = ctk.CTkButton(
self,
text="PLAY",
fg_color="green",
text_color="white",
width=200,
height=60,
corner_radius=20,
font=("Calibri", 16, "bold"),
command=lambda: controller.show_frame(GamePage)
)
button.pack(pady=70, side="bottom")
select_box = SelectBox(self, width=150, pady=50, side="bottom")
class GamePage(ctk.CTkFrame):
def __init__(self, parent, controller):
super().__init__(parent)
load_dotenv()
global client_id, client_secret
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
token = get_token()
playlist = get_playlist(token, "https://open.spotify.com/playlist/1R0DqNQD2IcnLX0PtLI8UM?si=91c22be80aa74b68")
tracks = playlist["tracks"]["items"]
songs = songs_for_game(tracks)
self.controller = controller
self.score = 0
self.score_label = ctk.CTkLabel(self, text=f"SCORE: {self.score}", font=("Calibri", 40))
self.score_label.place(x=20, y=10)
self.exit_button = ctk.CTkButton(self, text="EXIT",
height=50,
font=("Calibri", 36, "bold"),
fg_color="black",
command=lambda: controller.show_frame(StartPage))
self.exit_button.place(x=0, y=0)
self.update_button_position()
self.bind("<Configure>", self.on_resize)
self.playing(songs)
def playing(self, songs):
for song in songs:
frame1 = ctk.CTkFrame(self)
frame1.pack(pady=30)
img_url = song["album"]["images"][0]["url"]
song_name = song["album"]["name"]
artist = song["artists"][0]["name"]
response = requests.get(img_url)
image_data = io.BytesIO(response.content)
image_original = Image.open(image_data)
ctk_image = ctk.CTkImage(image_original, size=(500, 400))
label = ctk.CTkLabel(frame1, text="", image=ctk_image)
label.pack(pady=70)
name = ctk.CTkLabel(frame1, text=song_name, font=("Calibri", 32))
name.pack(pady=40)
self.string_var = tk.StringVar(value="")
entry = SongEntry(frame1, self.string_var)
entry.pack()
print(self.string_var.get().lower())
while self.string_var.get().lower() == "":
n = 0
if n == 2:
break
if self.string_var.get().lower() == artist.lower():
break
elif self.string_var.get().lower() == "":
continue
else:
n += 1
print(song_name)
def update_button_position(self):
window_width = self.winfo_width()
self.exit_button.place(x=window_width - 180, y=10)
def on_resize(self, event):
self.update_button_position()
class EndPage(ctk.CTkFrame):
def __init__(self, parent, controller):
super().__init__(parent)
self.controller = controller
self.label = ctk.CTkLabel(
self,
text="",
font=("Calibri", 68),
fg_color="green",
corner_radius=10,
text_color="white")
self.label.pack(pady=200)
button1 = ctk.CTkButton(self, text="PLAY AGAIN", command=lambda: controller.show_frame(GamePage), width=200, height=60)
button1.pack(pady=30)
button2 = ctk.CTkButton(self, text="MAIN MENU", command=lambda: controller.show_frame(StartPage), width=200, height=60)
button2.pack(pady=30)
def update_score(self, score):
self.label.configure(text=f"CONGRATULATIONS, YOUR SCORE IS {score}")
def main():
global score
score = 0
app = CustomTkinterApp()
app.title("GUESS THE SONG")
app.geometry("1280x800")
app.mainloop()
def get_token():
auth_string = client_id + ":" + client_secret
auth_bytes = auth_string.encode("utf-8")
auth_base64 = str(base64.b64encode(auth_bytes), "utf-8")
url = "https://accounts.spotify.com/api/token"
headers = {
"Authorization": "Basic " + auth_base64,
"Content-Type": "application/x-www-form-urlencoded"
}
data = {"grant_type": "client_credentials"}
result = requests.post(url, headers=headers, data=data)
json_result = json.loads(result.content)
token = json_result["access_token"]
return token
def get_auth_header(token):
return {"Authorization": "Bearer " + token}
def get_playlist(token, link):
url = "https://api.spotify.com/v1/playlists/"
headers = get_auth_header(token)
playlist_id = re.search(r"http(s)?://open.spotify.com/playlist/(.+)", link).group(2)
query_url = url + playlist_id + "/tracks"
result = requests.get(query_url, headers=headers)
json_result = json.loads(result.content)
return json_result
def score_track(scr, lvl=0):
return scr + lvl
def songs_for_game(tracks):
track_list = [d["track"] for d in tracks if "track" in d]
output = random.choices(track_list, k=5)
return output
if __name__ == "__main__":
main()
Pavle Vuksanovic is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.