Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.82 KB | None | 0 0
  1. """
  2.    SCOREBOARD Designed by Caleb Boese & Stuart Plunkett
  3.    for Andrew Huddleston
  4.    SCADA Lab #7
  5. """
  6. import csv
  7. from tkinter import *
  8. from PIL import Image, ImageTk
  9.  
  10. font_style_1 = ('Helvetica', 10)
  11. font_style_2 = ('Helvetica', 20)
  12. font_style_3 = ('Helvetica', 30)
  13.  
  14. # Scoreboard
  15. root = Tk()  # Instantiate the main scoreboard window
  16. root.title("Scoreboard")
  17. root["bg"] = "#4B7D0F"
  18.  
  19. # =============================== #
  20.  
  21. class Game:
  22.     """
  23.    Game object, contains all information for a game to be displayed on the scoreboard, as well as functionality for
  24.    committing the events into a CSV file to track.
  25.    """
  26.  
  27.     def __init__(self, home_team, away_team):
  28.         self.home_team = home_team  # "ottawa" "toronto" etc
  29.         self.away_team = away_team
  30.         self.goals = []  # container for goals
  31.         self.penalties = []  # container for penalties
  32.         self.home_team_shots = IntVar(root, 0)  # IntVar to allow updating live with Tk
  33.         self.away_team_shots = IntVar(root, 0)  # IntVar to allow updating live with Tk
  34.         self.time_elapsed = 0  # time since period start
  35.         self.current_period = IntVar(root, 1)  # current period
  36.         self.current_home_goals = IntVar(root, 0)
  37.         self.current_away_goals = IntVar(root, 0)
  38.         self.timer = StringVar(root)
  39.         self.set_game_time(0)
  40.         self.file_id = 0
  41.  
  42.     def goal(self, team, period, game_time, scorer, assist1, assist2):
  43.         self.goals.append([team, period, game_time, scorer, assist1, assist2])
  44.  
  45.     def penalty(self, team, period, game_time, player, infraction):
  46.         self.penalties.append([team, period, game_time, player, infraction])
  47.  
  48.     def set_game_time(self, value):
  49.         minutes = int((1200 - value) / 60)
  50.         seconds = int((1200 - value) % 60)
  51.         self.timer.set("{:d}:{:02d}".format(minutes, seconds))
  52.  
  53.     def return_total_goals(self, team):
  54.         i = 0
  55.         for goal in self.goals:
  56.             if goal[0] == team:
  57.                 i += 1
  58.  
  59.     def force_game_time(self, value):
  60.         time_value = value.get().split(':')
  61.         minutes = int(time_value[0])
  62.         seconds = int(time_value[1])
  63.         print("Forcing Game time to " + str(minutes) + " minutes and " + str(seconds) + " seconds")
  64.         total_time_elapsed_in_seconds = (minutes * 60) + seconds
  65.         print("Total Time in S: " + str(1200 - total_time_elapsed_in_seconds))
  66.         self.time_elapsed = 1200 - total_time_elapsed_in_seconds
  67.         self.set_game_time(self.time_elapsed)
  68.  
  69.     def return_home_goals(self):
  70.         self.current_home_goals.set(self.return_total_goals(self.home_team))
  71.  
  72.     def return_away_goals(self):
  73.         self.current_away_goals.set(self.return_total_goals(self.away_team))
  74.  
  75.     def add_home_shot(self):
  76.         self.home_team_shots.set(self.home_team_shots.get() + 1)
  77.         print("home shots increased to ")
  78.         print(self.home_team_shots.get())
  79.  
  80.     def add_away_shot(self):
  81.         self.away_team_shots.set(self.away_team_shots.get() + 1)
  82.         print("away shots increased to ")
  83.         print(self.away_team_shots.get())
  84.  
  85.     def power_play(self):
  86.         pass  # to be implemented once the timing module is finished
  87.  
  88.     def force_period(self, new_period):
  89.         self.current_period.set(new_period)
  90.         period_label_string.set(self.current_period.get())
  91.         print("forcing period to ")
  92.         print(self.current_period.get())
  93.  
  94.     def play(self):
  95.         pass
  96.  
  97.     def stop(self):
  98.         pass
  99.  
  100.     def assign_goal(self):
  101.         current_game.goal(goal_assign_team_select_value.get(), goal_assign_period_value,
  102.                           game_time_goal_assign_entry_value.get(), goal_assign_player_entry_value.get(),
  103.                           assist1_assign_player_entry_value.get(), assist2_assign_player_entry_value.get())
  104.         print(current_game.goals)
  105.         goal_assign_window.withdraw()
  106.  
  107.     def assign_penalty(self):
  108.         current_game.penalty(goal_assign_team_select_value.get(), goal_assign_period_value,
  109.                              game_time_goal_assign_entry_value.get(), penalty_assign_player_entry_value.get(),
  110.                              type_of_penalty_assign_entry_value.get())
  111.         print(current_game.penalties)
  112.         penalty_assign_window.withdraw()
  113.  
  114.     def commit_to_csv(self):
  115.         self.file_id += 1
  116.         filename = "Game #" + str(self.file_id) + ".csv"
  117.         with open(filename, 'w', newline='') as csv_file:
  118.             file_writer = csv.writer(csv_file, delimiter=',', quotechar='|', quoting=csv.QUOTE_MINIMAL)
  119.             for goal in self.goals:
  120.                 goal.insert(0, "goal")
  121.                 file_writer.writerow(goal)
  122.             for penalty in self.penalties:
  123.                 penalty.insert(0, "penalty")
  124.                 file_writer.writerow(penalty)
  125. # =============================== #
  126.  
  127.  
  128. current_game = Game("ottawa", "toronto")
  129.  
  130.  
  131. # =============================== #
  132.  
  133.  
  134. # Control Window
  135. ctrl = Toplevel()  # Instantiate the control window
  136. ctrl.title("Controls")
  137.  
  138. # Menu for Control Window
  139. ctrl_menu = Menu(ctrl)
  140. ctrl_menu.add_command(label="New", command=lambda: current_game.__init__("ottawa", "toronto"))
  141. ctrl_menu.add_command(label="Save to CSV", command=lambda: current_game.commit_to_csv())
  142. ctrl_menu.add_command(label="Quit", command=root.quit)
  143. ctrl.config(menu=ctrl_menu)
  144.  
  145. force_game_time_label = Label(ctrl, text='Force Game Time: ', font=font_style_1)
  146. force_game_time_label.grid(row=0, column=0)
  147. force_game_time_entry_value = StringVar()
  148. force_game_time_entry = Entry(ctrl, width=5, textvariable=force_game_time_entry_value)
  149. force_game_time_entry.grid(row=0, column=1)
  150. force_game_time_confirm = Button(ctrl, text='OK',
  151.                                  command=lambda: current_game.force_game_time(force_game_time_entry_value))
  152. force_game_time_confirm.grid(row=0, column=2)
  153.  
  154. force_period_label = Label(ctrl, text='Force Period: ', font=font_style_1)
  155. force_period_label.grid(row=1, column=0, rowspan=3)
  156. force_period_value = 1
  157. force_period_one_radio = Radiobutton(ctrl, text="1", variable=force_period_value, value=1)
  158. force_period_one_radio.grid(row=1, column=1)
  159. force_period_one_radio = Radiobutton(ctrl, text="2", variable=force_period_value, value=2)
  160. force_period_one_radio.grid(row=2, column=1)
  161. force_period_one_radio = Radiobutton(ctrl, text="3", variable=force_period_value, value=3)
  162. force_period_one_radio.grid(row=3, column=1)
  163. force_period_confirm = Button(ctrl, text="OK", command=lambda: current_game.force_period(force_period_value))
  164. force_period_confirm.grid(row=2, column=2)
  165.  
  166. add_away_shot_button = Button(ctrl, text='Add Away Shot', font=font_style_1,
  167.                               command=lambda: current_game.add_away_shot())
  168. add_away_shot_button.grid(row=4, column=0)
  169.  
  170. add_home_shot_button = Button(ctrl, text='Add Home Shot', font=font_style_1,
  171.                               command=lambda: current_game.add_home_shot())
  172. add_home_shot_button.grid(row=4, column=2)
  173.  
  174. add_goal_button = Button(ctrl, text='Goal', font=font_style_1, command=lambda: goal_assign_window.deiconify())
  175. add_goal_button.grid(row=5, column=0)
  176. add_penalty_button = Button(ctrl, text='Penalty', font=font_style_1, command=lambda: penalty_assign_window.deiconify())
  177. add_penalty_button.grid(row=5, column=2)
  178.  
  179. play_button = Button(ctrl, text='PLAY', font=font_style_2, command=lambda: current_game.play())
  180. play_button.grid(row=6, column=0)
  181. stop_button = Button(ctrl, text='STOP', font=font_style_2, command=lambda: current_game.stop())
  182. stop_button.grid(row=6, column=2)
  183.  
  184.  
  185. # =============================== #
  186.  
  187.  
  188. # Home/Away Team Labels
  189. away_team_image = ImageTk.PhotoImage(Image.open('bannerAway.bmp'))
  190. away_team_image_label = Label(root, image=away_team_image, border=0)
  191. away_team_image_label.grid(row=0, column=0)
  192. home_team_image = ImageTk.PhotoImage(Image.open('bannerHome.bmp'))
  193. home_team_image_label = Label(root, image=home_team_image, border=0)
  194. home_team_image_label.grid(row=0, column=3)
  195.  
  196. # Game Time Label
  197. game_timer_label = Label(root, textvariable=current_game.timer, font=font_style_3, bg='black', fg='white')
  198. game_timer_label.grid(row=0, column=1, rowspan=2, columnspan=2)
  199.  
  200. # Away Team Score
  201. away_team_score_label = Label(root, text=current_game.current_away_goals.get(),
  202.                               font=font_style_3, bg='black', fg='white')
  203. away_team_score_label.grid(row=1, column=0)
  204.  
  205. # Home Team Score
  206. home_team_score_label = Label(root, text=current_game.current_home_goals.get(),
  207.                               font=font_style_3, bg='black', fg='white')
  208. home_team_score_label.grid(row=1, column=3)
  209.  
  210. # Period Label
  211. period_image = ImageTk.PhotoImage(Image.open('bannerPeriod.bmp'))
  212. period_image_label = Label(root, image=period_image, border=0)
  213. period_image_label.grid(row=2, column=1, columnspan=2)
  214.  
  215. # Period Indicator
  216. period_label_string = StringVar()
  217. period_label = Label(root, textvariable=period_label_string.get(), font=font_style_3, bg='black', fg='white')
  218. period_label.grid(row=3, column=1, columnspan=2)
  219.  
  220. # Penalty Indicators
  221. penalty_image = ImageTk.PhotoImage(Image.open('bannerPenalty.bmp'))
  222. home_penalty_image_label = Label(root, image=penalty_image, border=0)
  223. home_penalty_image_label.grid(row=3, column=0)
  224. away_penalty_image_label = Label(root, image=penalty_image, border=0)
  225. away_penalty_image_label.grid(row=3, column=3)
  226.  
  227. # Away Penalty
  228. away_penalty1_label = Label(root, text='0:00', font=font_style_3, bg='black', fg='white')
  229. away_penalty1_label.grid(row=4, column=0)
  230. away_penalty2_label = Label(root, text='0:00', font=font_style_3, bg='black', fg='white')
  231. away_penalty2_label.grid(row=5, column=0)
  232.  
  233. # Home Penalty
  234. home_penalty1_label = Label(root, text='0:00', font=font_style_3, bg='black', fg='white')
  235. home_penalty1_label.grid(row=4, column=3)
  236. home_penalty2_label = Label(root, text='0:00', font=font_style_3, bg='black', fg='white')
  237. home_penalty2_label.grid(row=5, column=3)
  238.  
  239. # Shots on Goal Indicator
  240. shots_image = ImageTk.PhotoImage(Image.open('bannerShots.bmp'))
  241. shots_image_label = Label(root, image=shots_image, border=0)
  242. shots_image_label.grid(row=4, column=1, columnspan=2)
  243.  
  244. # Away Team Shots
  245. away_team_shots_string = StringVar()
  246. away_team_shots_string.set(current_game.away_team_shots)
  247. away_team_shots_label = Label(root, textvariable=away_team_shots_string.get(),
  248.                               font=font_style_3, bg='black', fg='white')
  249. away_team_shots_label.grid(row=5, column=1)
  250.  
  251. # Home Team Shots
  252. home_team_shots_string = StringVar()
  253. home_team_shots_string.set(current_game.home_team_shots)
  254. home_team_shots_label = Label(root, textvariable=home_team_shots_string.get(),
  255.                               font=font_style_3, bg='black', fg='white')
  256. home_team_shots_label.grid(row=5, column=2)
  257.  
  258. # =============================== #
  259.  
  260. # Goal Assignment Window
  261. goal_assign_window = Toplevel()
  262. goal_assign_window.title("Goal Assignment")
  263. goal_assign_window.withdraw()
  264. goal_assign_player_team_label = Label(goal_assign_window, text='Team: ')
  265. goal_assign_player_team_label.grid(row=0, column=0)
  266. goal_assign_team_select_value = StringVar()
  267. goal_assign_team_select1 = Radiobutton(goal_assign_window, text="Home",
  268.                                        variable=goal_assign_team_select_value, value='Home', width=10)
  269. goal_assign_team_select1.grid(row=0, column=1)
  270. goal_assign_team_select2 = Radiobutton(goal_assign_window, text="Away",
  271.                                        variable=goal_assign_team_select_value, value='Away', width=10)
  272. goal_assign_team_select2.grid(row=0, column=2)
  273. goal_assign_player_label = Label(goal_assign_window, text='Scoring Player: ')
  274. goal_assign_player_label.grid(row=1, column=0)
  275. goal_assign_player_entry_value = StringVar()
  276. goal_assign_player_entry = Entry(goal_assign_window, textvariable=goal_assign_player_entry_value, width=2)
  277. goal_assign_player_entry.grid(row=1, column=1)
  278.  
  279. assist1_assign_player_label = Label(goal_assign_window, text='Assist1 Player: ')
  280. assist1_assign_player_label.grid(row=2, column=0)
  281. assist1_assign_player_entry_value = StringVar()
  282. assist1_assign_player_entry = Entry(goal_assign_window, textvariable=assist1_assign_player_entry_value, width=2)
  283. assist1_assign_player_entry.grid(row=2, column=1)
  284.  
  285. assist2_assign_player_label = Label(goal_assign_window, text='Assist2 Player: ')
  286. assist2_assign_player_label.grid(row=3, column=0)
  287. assist2_assign_player_entry_value = StringVar()
  288. assist2_assign_player_entry = Entry(goal_assign_window, textvariable=assist2_assign_player_entry_value, width=2)
  289. assist2_assign_player_entry.grid(row=3, column=1)
  290.  
  291. game_time_goal_assign = Label(goal_assign_window, text='Game Time for Goal: ')
  292. game_time_goal_assign.grid(row=4, column=0)
  293. game_time_goal_assign_entry_value = StringVar()
  294. game_time_goal_assign_player_entry = Entry(goal_assign_window, textvariable=game_time_goal_assign_entry_value, width=5)
  295. game_time_goal_assign_player_entry.grid(row=4, column=1)
  296.  
  297. goal_assign_period_label = Label(goal_assign_window, text='Period: ', font=font_style_1)
  298. goal_assign_period_label.grid(row=5, column=0, rowspan=3)
  299. goal_assign_period_value = 1
  300. goal_assign_period_one_radio = Radiobutton(goal_assign_window, text="1", variable=goal_assign_period_value, value="g1")
  301. goal_assign_period_one_radio.grid(row=5, column=1)
  302. goal_assign_period_one_radio = Radiobutton(goal_assign_window, text="2", variable=goal_assign_period_value, value="g2")
  303. goal_assign_period_one_radio.grid(row=6, column=1)
  304. goal_assign_period_one_radio = Radiobutton(goal_assign_window, text="3", variable=goal_assign_period_value, value="g3")
  305. goal_assign_period_one_radio.grid(row=7, column=1)
  306.  
  307. goal_assign_player_entry_confirm = Button(goal_assign_window, text="OK", command=lambda: current_game.assign_goal())
  308. goal_assign_player_entry_confirm.grid(row=8, column=0, columnspan=3)
  309.  
  310. # Penalty Assignment Window
  311. penalty_assign_window = Toplevel()
  312. penalty_assign_window.withdraw()
  313. penalty_assign_window.title("Penalty Assignment")
  314.  
  315. penalty_assign_player_team_label = Label(penalty_assign_window, text='Team: ')
  316. penalty_assign_player_team_label.grid(row=0, column=0)
  317. penalty_assign_team_select_value = StringVar()
  318. penalty_assign_team_select1 = Radiobutton(penalty_assign_window, text="Home",
  319.                                           variable=penalty_assign_team_select_value, value='Home', width=10)
  320. penalty_assign_team_select1.grid(row=0, column=1)
  321. penalty_assign_team_select2 = Radiobutton(penalty_assign_window, text="Away",
  322.                                           variable=penalty_assign_team_select_value, value='Away', width=10)
  323. penalty_assign_team_select2.grid(row=0, column=2)
  324. penalty_assign_player_label = Label(penalty_assign_window, text='Penalized Player: ')
  325. penalty_assign_player_label.grid(row=1, column=0)
  326. penalty_assign_player_entry_value = StringVar()
  327. penalty_assign_player_entry = Entry(penalty_assign_window, textvariable=penalty_assign_player_entry_value, width=2)
  328. penalty_assign_player_entry.grid(row=1, column=1)
  329.  
  330. game_time_penalty_assign = Label(penalty_assign_window, text='Game Time for Penalty: ')
  331. game_time_penalty_assign.grid(row=4, column=0)
  332. game_time_penalty_assign_entry_value = StringVar()
  333. game_time_penalty_assign_player_entry = Entry(penalty_assign_window,
  334.                                               textvariable=game_time_penalty_assign_entry_value, width=5)
  335. game_time_penalty_assign_player_entry.grid(row=4, column=1)
  336.  
  337. type_of_penalty_assign = Label(penalty_assign_window, text='Type of Penalty: ')
  338. type_of_penalty_assign.grid(row=5, column=0)
  339. type_of_penalty_assign_entry_value = StringVar()
  340. type_of_penalty_assign_player_entry = Entry(penalty_assign_window,
  341.                                             textvariable=type_of_penalty_assign_entry_value, width=10)
  342. type_of_penalty_assign_player_entry.grid(row=5, column=1)
  343.  
  344. penalty_assign_period_label = Label(penalty_assign_window, text='Period: ', font=font_style_1)
  345. penalty_assign_period_label.grid(row=6, column=0)
  346. penalty_assign_period_value = 1
  347. penalty_assign_period_one_radio = Radiobutton(penalty_assign_window,
  348.                                               text="1", variable=penalty_assign_period_value, value="p1")
  349. penalty_assign_period_one_radio.grid(row=6, column=1)
  350. penalty_assign_period_one_radio = Radiobutton(penalty_assign_window,
  351.                                               text="2", variable=penalty_assign_period_value, value="p2")
  352. penalty_assign_period_one_radio.grid(row=7, column=1)
  353. penalty_assign_period_one_radio = Radiobutton(penalty_assign_window,
  354.                                               text="3", variable=penalty_assign_period_value, value="p3")
  355. penalty_assign_period_one_radio.grid(row=8, column=1)
  356.  
  357. penalty_assign_player_entry_confirm = Button(penalty_assign_window,
  358.                                              text="OK", command=lambda: current_game.assign_penalty())
  359. penalty_assign_player_entry_confirm.grid(row=9, column=0, columnspan=3)
  360.  
  361.  
  362. # =============================== #
  363.  
  364. # Main Loop
  365. root.mainloop()
  366.  
  367. # =============================== #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement