Monday, March 18, 2019

codingdirectional: Get only the latest live match from NBA with python

Hello and welcome back, in this chapter we will continue to develop the previous sports score application by showing all the latest live matches from the NBA on the text widget area. The sports.py module does not have a method to only return the live NBA matches but instead that API call will return basically all the live basketball matches from all the basketball leagues around the world. Therefore we will need to filter out all those none NBA results before showing the data on the text widget. Below is the modified version of the program which will do just that. We will perform the filtering process under the get matches method.

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 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")

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)

win.mainloop()

If you start the software and click on the All Matches button you will see all the live matches from NBA in the text widget area. Like, share or follow me on Twitter.

Download mine latest Free Firefox Extension on Firefox Store to show your love for my project.

Due to the tight daily schedule, this site will switch to twice a week updating starting from today onward.



from Planet Python
via read more

No comments:

Post a Comment

TestDriven.io: Working with Static and Media Files in Django

This article looks at how to work with static and media files in a Django project, locally and in production. from Planet Python via read...