Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- import csv
- import random
- import time
- Ufieldnames = ['name', 'password']
- Sfieldnames = ['artist', 'song']
- HSfieldnames = ['name', 'score']
- points = 0
- def initialise_window():
- global root
- root = Tk()
- root.lift()
- root.attributes("-topmost", True)
- root.title("Songs Quiz")
- root.geometry('+500+500')
- def kill():
- global root
- root.destroy()
- def end():
- exit()
- def run_again():
- global root
- points = 0
- root.destroy()
- run()
- def correct(value):
- global root
- global points
- points = points+value
- initialise_window()
- Title = Label(root, text= 'Correct!', font = ('Times',18))
- Title.grid(row = 0, column = 0)
- root.after(1000, lambda: root.destroy())
- root.mainloop()
- run()
- def start():
- global root
- global username
- global Ufieldnames
- initialise_window()
- root.username = StringVar()
- root.password = StringVar()
- Title = Label(root, text= 'Welcome', font = ('Times',20, "underline"))
- Title.grid(row = 0, column = 1)
- Title = Label(root, text="Username", font = ('Times',12))
- Title.grid(row = 1, column = 0)
- username_entry = Entry(root, textvariable=root.username,font = ('Times',12))
- username_entry.grid(row = 1, column = 1)
- Title = Label(root, text="password", font = ('Times',12))
- Title.grid(row = 2, column = 0)
- password_entry = Entry(root, textvariable = root.password, font = ('Times',12))
- password_entry.grid(row = 2, column = 1)
- Exit = Button(root, text="OK", fg="Black", command=kill, font = ('Times',12))
- Exit.grid(row = 3, column = 1)
- End = Button(root, text="Cancel", fg="Black", command=end, font = ('Times',12))
- End.grid(row = 3, column = 0)
- root.mainloop()
- username = root.username.get()
- password = root.password.get()
- with open('users.csv', 'r') as csvfile:
- authorised = False
- reader = csv.DictReader(csvfile, fieldnames = Ufieldnames)
- for row in reader:
- if row['name'] == username and row['password'] == password:
- authorised = True
- if authorised == False:
- initialise_window()
- Title = Label(root, text= 'sorry those details are incorrect', font = ('Times',12))
- Title.grid(row = 0, column = 0)
- root.after(2000, lambda: root.destroy())
- root.mainloop()
- start()
- else:
- run()
- def run():
- global root
- global Sfieldnames
- global HSfieldnames
- global points
- global username
- with open('songs.csv', 'r') as csvfile:
- reader = csv.DictReader(csvfile, fieldnames = Sfieldnames)
- X = 0
- for row in reader:
- X = X + 1
- entry = random.randint(1, X - 1)
- with open('songs.csv', 'r') as csvfile:
- X = -1
- reader = csv.DictReader(csvfile, fieldnames = Sfieldnames)
- for row in reader:
- X = X + 1
- if X == entry:
- artist = row["artist"]
- song = row["song"]
- question = ""
- NLINW = True #Next Letter is new word
- for X in song:
- if NLINW == True:
- question = question + X + " "
- NLINW = False
- elif X == " ":
- question = question + " "
- NLINW = True
- else:
- question = question + "_"
- initialise_window()
- root.song_name = StringVar()
- Title = Label(root, text= 'What is this songs name?', font = ('Times',20, "underline"))
- Title.grid(row = 0, column = 0)
- Title = Label(root, text=artist+": "+question, font = ('Times',15))
- Title.grid(row = 1, column = 0)
- song_name_entry = Entry(root, textvariable=root.song_name,font = ('Times',12))
- song_name_entry.grid(row = 2, column = 0)
- Exit = Button(root, text="OK", fg="Black", command=kill, font = ('Times',12))
- Exit.grid(row = 3, column = 0)
- root.mainloop()
- answer = root.song_name.get()
- if answer.lower() == song.lower():
- correct(3)
- else:
- initialise_window()
- Title = Label(root, text= 'Incorrect, try again', font = ('Times',18, "underline"))
- Title.grid(row = 0, column = 0)
- root.song_name = StringVar()
- Title = Label(root, text= 'What is this songs name?', font = ('Times',15, "underline"))
- Title.grid(row = 1, column = 0)
- Title = Label(root, text=artist+": "+question, font = ('Times',15))
- Title.grid(row = 2, column = 0)
- song_name_entry = Entry(root, textvariable=root.song_name,font = ('Times',12))
- song_name_entry.grid(row = 3, column = 0)
- Exit = Button(root, text="OK", fg="Black", command=kill, font = ('Times',12))
- Exit.grid(row = 4, column = 0)
- root.mainloop()
- answer = root.song_name.get()
- if answer.lower() == song.lower():
- correct(1)
- else:
- initialise_window()
- Title = Label(root, text= 'sorry, that is incorrect, game over!', font = ('Times',18, "underline"))
- Title.grid(row = 0, column = 0)
- Title = Label(root, text= 'It was: '+song, font = ('Times',15, "underline"))
- Title.grid(row = 1, column = 0)
- Title = Label(root, text= 'You scored : '+str(points)+' points!', font = ('Times',15, "underline"))
- Title.grid(row = 2, column = 0)
- root.after(2000, lambda: root.destroy()) #taken from stack overflow https://stackoverflow.com/questions/15306222/automatically-close-window-after-a-certain-time
- root.mainloop()
- with open('scores.csv', 'a') as csvfile:
- writer = csv.DictWriter(csvfile, fieldnames=HSfieldnames)
- writer.writerow({'name':username, 'score':points})
- with open('scores.csv', 'r') as csvfile:
- reader = csv.DictReader(csvfile, fieldnames = HSfieldnames)
- scores = []
- for row in reader:
- if row['score'] != '':
- scores.append(int(row['score']))
- scores.sort(reverse = True)
- scores = scores[0:5]
- with open('scores.csv', 'r') as csvfile:
- reader = csv.DictReader(csvfile, fieldnames = HSfieldnames)
- scorenames = ["-", "-", "-", "-", "-"]
- for row in reader:
- if row['score'] != '':
- if int(row['score']) in scores:
- A = int(row['score'])
- place = scores.index(A) #taken from tutorials point https://www.tutorialspoint.com/python/list_index.htm
- scorenames[place] = row['name'] #taken from tutorials point https://www.tutorialspoint.com/python/list_index.htm
- initialise_window()
- for A in range(0, 5):
- HS = Label(root, text= "In place #"+str(A+1)+": "+scorenames[A]+ " with "+ str(scores[A])+ " points", font = ('Times',15))
- HS.grid(row = A, column = 0)
- root.after(4000, lambda: root.destroy())
- root.mainloop()
- initialise_window()
- L1 = Label(root, text = "Play again?",font = ('Times',15))
- L1.grid(row = 0, column = 0)
- Exit = Button(root, text="No", fg="Black", command=kill, font = ('Times',12))
- Exit.grid(row = 1, column = 0)
- PA = Button(root, text="Yes", fg="Black", command=run_again, font = ('Times',12))
- PA.grid(row = 1, column = 1)
- start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement