Welcome back to this latest NBA application project. In this chapter, we will create another method which will do the same thing that the previous method does which is to display the latest live NBA matches in the text widget, but this time it will use the all matches method instead from the sports module which will return all the sport matches as well as all the world basketball leagues matches. As usual, we will need to filter out the unwanted basketball scores from the other basketball leagues.
Below is the revised version of the project.
import sports
import json
from tkinter import *
import tkinter.ttk as tk
import datetime
win = Tk() # Create tk instance
win.title("NBA") # Add a title
win.resizable(0, 0) # Disable resizing the GUI
win.configure(background='white') # change window background color
selectorFrame = Frame(win, background="white") # create top frame to hold team 1 vs team 2 combobox
selectorFrame.pack(anchor = "nw", pady = 2, padx=10)
match_label = Label(selectorFrame, text = "Select Team 1 vs Team 2 :", background="white")
match_label.pack(anchor="w") # the team label
# Create a combo box for team 1
team1 = tk.Combobox(selectorFrame)
team1.pack(side = LEFT, padx=3)
# Create a combo box for team 2
team2 = tk.Combobox(selectorFrame)
team2.pack(side = LEFT, padx=3)
s = StringVar() # create string variable
# create match frame and text widget to display the incoming match data
matchFrame = Frame(win)
matchFrame.pack(side=TOP)
match = Label(matchFrame)
match.pack()
text_widget = Text(match, fg='white', background='black')
text_widget.pack()
s.set("Click below buttons to find out the match result")
text_widget.insert(END, s.get())
buttonFrame = Frame(win) # create a bottom frame to hold the find button
buttonFrame.pack(side = BOTTOM, fill=X, pady = 6)
# fill up the combo boxes with the team name data from the text file
team_tuple = tuple()
f = open("TextFile1.txt", "r")
nba_list = [] # will be used to filter out the unwanted team from the other league
for line in f.readlines():
line = line.replace('\n', '')
nba_list.append(line)
team_tuple += (line, )
f.close()
team1["values"] = team_tuple
team1.current(1)
team2["values"] = team_tuple
team2.current(0)
def get_matches(): # show all the live NBA team matches
match_str = ' Live NBA Matches ' + str(datetime.datetime.now()) + '\n'
try:
matches = sports.get_sport(sports.BASKETBALL)
for item in matches:
match_all = str(item)
for nba_team in nba_list:
if(nba_team in match_all):
match_str += (match_all) + '\n'
break
text_widget.delete('1.0', END) # clear all those previous text first
s.set(match_str)
text_widget.insert(INSERT, s.get()) # display teams match data in text widget
except:
print("An exception occurred")
def get_match(): # return the recent match of team 1 vs team 2
try:
match = sports.get_match(sports.BASKETBALL, team1.get(), team2.get())
text_widget.delete('1.0', END) # clear all those previous text first
s.set(match)
text_widget.insert(INSERT, s.get()) # display team match data in text widget
except:
print("An exception occurred")
def get_old_matches(): # display all the old matches
match_str = ' Live NBA Matches ' + str(datetime.datetime.now()) + '\n'
try:
all_matches = sports.all_matches()
basketball = all_matches['basketball'] # only basketball match
for item in basketball:
match_all = str(item)
for nba_team in nba_list:
if(nba_team in match_all):
match_str += (match_all) + '\n'
break
text_widget.delete('1.0', END) # clear all those previous text first
s.set(match_str)
text_widget.insert(INSERT, s.get()) # display teams match data in text widget
except:
print("An exception occurred")
action_vid = tk.Button(buttonFrame, text="Latest Match", command=get_match) # button used to find out the team match data
action_vid.pack(side=LEFT, padx=2)
action_vid1 = tk.Button(buttonFrame, text="All Matches", command=get_matches) # button used to find out the teams match data
action_vid1.pack(side=LEFT, padx=2)
action_vid2 = tk.Button(buttonFrame, text="Live Matches", command=get_old_matches) # button used to find out the teams live match data
action_vid2.pack(side=LEFT, padx=2)
win.mainloop()
If you run the above program and click on the live matches button then you will see the following results.
Create a live basketball score application with #python pic.twitter.com/OvFog9NVhm
— TechLikin (@ChooWhei) March 26, 2019
Like, share or follow me on Twitter.
from Planet Python
via read more
No comments:
Post a Comment