codingdirectional: Create a sports score application with python

Hello and welcome back. In this chapter, we will create a small sports score application with the help of the sports.py module. We will create a simple application to retrieve the score of the NBA result for any two particular teams and prints it on the tkinter’s text widget. Before we start, you will need to download the sports.py module to your own computer. Since the module is on pypi.org all you need is to open up your windows command prompt and type in below line to download that module! As I have mentioned earlier, I am now using visual studio 2019 to develop this new python project.

 pip install sports.py 

Now just start the visual studio 2019 then type the below code into the code editor.

 import sports import json from tkinter import * import tkinter.ttk as tk  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 the find button 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") for line in f.readlines():     line = line.replace('\n', '')     team_tuple += (line, ) f.close()  team1["values"] = team_tuple team1.current(1) team2["values"] = team_tuple team2.current(0)  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="Find", command=get_match) # button used to find out the team match data action_vid.pack()  win.mainloop() 

Select the debug tab then start debugging the program.

In the next chapter, we will further modify this sports score application, maybe with other module and API because there are a few shortages in this current module.

Planet Python

Leave a Reply

Your email address will not be published. Required fields are marked *