Advertisement
Guest User

Untitled

a guest
Dec 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. from tkinter import *
  2. import csv
  3. import random
  4. import time
  5. Ufieldnames = ['name', 'password']
  6. Sfieldnames = ['artist', 'song']
  7. HSfieldnames = ['name', 'score']
  8. points = 0
  9. def initialise_window():
  10. global root
  11. root = Tk()
  12. root.lift()
  13. root.attributes("-topmost", True)
  14. root.title("Songs Quiz")
  15. root.geometry('+500+500')
  16. def kill():
  17. global root
  18. root.destroy()
  19. def end():
  20. exit()
  21. def run_again():
  22. global root
  23. points = 0
  24. root.destroy()
  25. run()
  26. def correct(value):
  27. global root
  28. global points
  29. points = points+value
  30. initialise_window()
  31. Title = Label(root, text= 'Correct!', font = ('Times',18))
  32. Title.grid(row = 0, column = 0)
  33. root.after(1000, lambda: root.destroy())
  34. root.mainloop()
  35. run()
  36. def start():
  37. global root
  38. global username
  39. global Ufieldnames
  40. initialise_window()
  41. root.username = StringVar()
  42. root.password = StringVar()
  43. Title = Label(root, text= 'Welcome', font = ('Times',20, "underline"))
  44. Title.grid(row = 0, column = 1)
  45. Title = Label(root, text="Username", font = ('Times',12))
  46. Title.grid(row = 1, column = 0)
  47. username_entry = Entry(root, textvariable=root.username,font = ('Times',12))
  48. username_entry.grid(row = 1, column = 1)
  49. Title = Label(root, text="password", font = ('Times',12))
  50. Title.grid(row = 2, column = 0)
  51. password_entry = Entry(root, textvariable = root.password, font = ('Times',12))
  52. password_entry.grid(row = 2, column = 1)
  53. Exit = Button(root, text="OK", fg="Black", command=kill, font = ('Times',12))
  54. Exit.grid(row = 3, column = 1)
  55. End = Button(root, text="Cancel", fg="Black", command=end, font = ('Times',12))
  56. End.grid(row = 3, column = 0)
  57. root.mainloop()
  58. username = root.username.get()
  59. password = root.password.get()
  60. with open('users.csv', 'r') as csvfile:
  61. authorised = False
  62. reader = csv.DictReader(csvfile, fieldnames = Ufieldnames)
  63. for row in reader:
  64. if row['name'] == username and row['password'] == password:
  65. authorised = True
  66. if authorised == False:
  67. initialise_window()
  68. Title = Label(root, text= 'sorry those details are incorrect', font = ('Times',12))
  69. Title.grid(row = 0, column = 0)
  70. root.after(2000, lambda: root.destroy())
  71. root.mainloop()
  72. start()
  73. else:
  74. run()
  75. def run():
  76. global root
  77. global Sfieldnames
  78. global HSfieldnames
  79. global points
  80. global username
  81. with open('songs.csv', 'r') as csvfile:
  82. reader = csv.DictReader(csvfile, fieldnames = Sfieldnames)
  83. X = 0
  84. for row in reader:
  85. X = X + 1
  86. entry = random.randint(1, X - 1)
  87. with open('songs.csv', 'r') as csvfile:
  88. X = -1
  89. reader = csv.DictReader(csvfile, fieldnames = Sfieldnames)
  90. for row in reader:
  91. X = X + 1
  92. if X == entry:
  93. artist = row["artist"]
  94. song = row["song"]
  95. question = ""
  96. NLINW = True #Next Letter is new word
  97. for X in song:
  98. if NLINW == True:
  99. question = question + X + " "
  100. NLINW = False
  101. elif X == " ":
  102. question = question + " "
  103. NLINW = True
  104. else:
  105. question = question + "_"
  106. initialise_window()
  107. root.song_name = StringVar()
  108. Title = Label(root, text= 'What is this songs name?', font = ('Times',20, "underline"))
  109. Title.grid(row = 0, column = 0)
  110. Title = Label(root, text=artist+": "+question, font = ('Times',15))
  111. Title.grid(row = 1, column = 0)
  112. song_name_entry = Entry(root, textvariable=root.song_name,font = ('Times',12))
  113. song_name_entry.grid(row = 2, column = 0)
  114. Exit = Button(root, text="OK", fg="Black", command=kill, font = ('Times',12))
  115. Exit.grid(row = 3, column = 0)
  116. root.mainloop()
  117. answer = root.song_name.get()
  118. if answer.lower() == song.lower():
  119. correct(3)
  120. else:
  121. initialise_window()
  122. Title = Label(root, text= 'Incorrect, try again', font = ('Times',18, "underline"))
  123. Title.grid(row = 0, column = 0)
  124. root.song_name = StringVar()
  125. Title = Label(root, text= 'What is this songs name?', font = ('Times',15, "underline"))
  126. Title.grid(row = 1, column = 0)
  127. Title = Label(root, text=artist+": "+question, font = ('Times',15))
  128. Title.grid(row = 2, column = 0)
  129. song_name_entry = Entry(root, textvariable=root.song_name,font = ('Times',12))
  130. song_name_entry.grid(row = 3, column = 0)
  131. Exit = Button(root, text="OK", fg="Black", command=kill, font = ('Times',12))
  132. Exit.grid(row = 4, column = 0)
  133. root.mainloop()
  134. answer = root.song_name.get()
  135. if answer.lower() == song.lower():
  136. correct(1)
  137. else:
  138. initialise_window()
  139. Title = Label(root, text= 'sorry, that is incorrect, game over!', font = ('Times',18, "underline"))
  140. Title.grid(row = 0, column = 0)
  141. Title = Label(root, text= 'It was: '+song, font = ('Times',15, "underline"))
  142. Title.grid(row = 1, column = 0)
  143. Title = Label(root, text= 'You scored : '+str(points)+' points!', font = ('Times',15, "underline"))
  144. Title.grid(row = 2, column = 0)
  145. root.after(2000, lambda: root.destroy()) #taken from stack overflow https://stackoverflow.com/questions/15306222/automatically-close-window-after-a-certain-time
  146. root.mainloop()
  147. with open('scores.csv', 'a') as csvfile:
  148. writer = csv.DictWriter(csvfile, fieldnames=HSfieldnames)
  149. writer.writerow({'name':username, 'score':points})
  150. with open('scores.csv', 'r') as csvfile:
  151. reader = csv.DictReader(csvfile, fieldnames = HSfieldnames)
  152. scores = []
  153. for row in reader:
  154. if row['score'] != '':
  155. scores.append(int(row['score']))
  156. scores.sort(reverse = True)
  157. scores = scores[0:5]
  158. with open('scores.csv', 'r') as csvfile:
  159. reader = csv.DictReader(csvfile, fieldnames = HSfieldnames)
  160. scorenames = ["-", "-", "-", "-", "-"]
  161. for row in reader:
  162. if row['score'] != '':
  163. if int(row['score']) in scores:
  164. A = int(row['score'])
  165. place = scores.index(A) #taken from tutorials point https://www.tutorialspoint.com/python/list_index.htm
  166. scorenames[place] = row['name'] #taken from tutorials point https://www.tutorialspoint.com/python/list_index.htm
  167. initialise_window()
  168. for A in range(0, 5):
  169. HS = Label(root, text= "In place #"+str(A+1)+": "+scorenames[A]+ " with "+ str(scores[A])+ " points", font = ('Times',15))
  170. HS.grid(row = A, column = 0)
  171. root.after(4000, lambda: root.destroy())
  172. root.mainloop()
  173. initialise_window()
  174. L1 = Label(root, text = "Play again?",font = ('Times',15))
  175. L1.grid(row = 0, column = 0)
  176. Exit = Button(root, text="No", fg="Black", command=kill, font = ('Times',12))
  177. Exit.grid(row = 1, column = 0)
  178. PA = Button(root, text="Yes", fg="Black", command=run_again, font = ('Times',12))
  179. PA.grid(row = 1, column = 1)
  180. start()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement