Build an NBA Team Information GUI with Python and Tkinter

View all coding sources

I created an NBA team database query program using Python GUI.


NBA team database query program

Description: When a user selects an NBA team list, the program shows basic information about the team (e.g. hometown, year of establishment, representative players).


Functions:


Select a team from the drop-down menu.


Display simple information about the selected team.


Extensibility:


Add player information or season performance. 







import tkinter as tk
from tkinter import ttk

# NBA Team Data (Dictionary)
nba_teams = {
    "Los Angeles Lakers": {
        "city": "Los Angeles",
        "founded": 1947,
        "key_player": "LeBron James",
        "championships": 17
    },
    "Boston Celtics": {
        "city": "Boston",
        "founded": 1946,
        "key_player": "Jayson Tatum",
        "championships": 17
    },
    "Golden State Warriors": {
        "city": "San Francisco",
        "founded": 1946,
        "key_player": "Stephen Curry",
        "championships": 7
    },
    "Chicago Bulls": {
        "city": "Chicago",
        "founded": 1966,
        "key_player": "DeMar DeRozan",
        "championships": 6
    },
    "Miami Heat": {
        "city": "Miami",
        "founded": 1988,
        "key_player": "Jimmy Butler",
        "championships": 3
    },
    "Milwaukee Bucks": {
        "city": "Milwaukee",
        "founded": 1968,
        "key_player": "Giannis Antetokounmpo",
        "championships": 2
    },
    "Phoenix Suns": {
        "city": "Phoenix",
        "founded": 1968,
        "key_player": "Kevin Durant",
        "championships": 0
    },
    "Denver Nuggets": {
        "city": "Denver",
        "founded": 1974,
        "key_player": "Nikola Jokic",
        "championships": 1
    },
    "Philadelphia 76ers": {
        "city": "Philadelphia",
        "founded": 1939,
        "key_player": "Joel Embiid",
        "championships": 3
    },
    "Dallas Mavericks": {
        "city": "Dallas",
        "founded": 1980,
        "key_player": "Luka Dončić",
        "championships": 1
    }
}


def create_team_info_gui():
    """Creates the GUI for displaying NBA team information."""

    root = tk.Tk()
    root.title("NBA Team Information")

    # Team Selection Dropdown
    team_label = ttk.Label(root, text="Select a Team:")
    team_label.grid(row=0, column=0, padx=10, pady=10, sticky=tk.W)

    team_names = list(nba_teams.keys())
    selected_team = tk.StringVar(root)
    selected_team.set(team_names[0])  # Default selection

    team_dropdown = ttk.Combobox(root, textvariable=selected_team, values=team_names, state="readonly")
    team_dropdown.grid(row=0, column=1, padx=10, pady=10, sticky=tk.W)

    # Information Display
    info_label = ttk.Label(root, text="Team Information:")
    info_label.grid(row=1, column=0, padx=10, pady=5, sticky=tk.W)

    info_text = tk.Text(root, height=10, width=40)
    info_text.grid(row=1, column=1, padx=10, pady=5, sticky=tk.W)
    info_text.config(state=tk.DISABLED)  # Make it read-only

    def update_info():
        """Updates the information display based on the selected team."""
        team_name = selected_team.get()
        team_data = nba_teams[team_name]

        info = f"City: {team_data['city']}\n"
        info += f"Founded: {team_data['founded']}\n"
        info += f"Key Player: {team_data['key_player']}\n"
        info += f"Championships: {team_data['championships']}\n"

        info_text.config(state=tk.NORMAL)  # Enable editing temporarily
        info_text.delete("1.0", tk.END)  # Clear previous text
        info_text.insert(tk.END, info)
        info_text.config(state=tk.DISABLED)  # Disable editing again

    # Button to Update Information
    update_button = ttk.Button(root, text="Show Information", command=update_info)
    update_button.grid(row=2, column=0, columnspan=2, pady=10)

    root.mainloop()


if __name__ == "__main__":
    create_team_info_gui()

-

Key improvements and explanations:

  • Data Structure: Uses a dictionary (nba_teams) to store team information. This makes it easy to access data by team name.
  • Tkinter GUI: Creates a basic GUI with a dropdown menu (Combobox) for team selection and a text area to display information.
  • update_info() Function: This function is called when the "Show Information" button is clicked. It retrieves the selected team's data from the nba_teams dictionary and updates the text area with the information.
  • Read-Only Text Area: The info_text widget is configured to be read-only (state=tk.DISABLED) to prevent the user from accidentally modifying the information.
  • Clearer Layout: Uses grid layout manager with padx, pady, and sticky options for better spacing and alignment.
  • Default Selection: Sets a default team in the dropdown menu.
  • Comments: Includes comments to explain the code.

How to run:

  1. Save the code as a .py file (e.g., nba_team_info.py).
  2. Run the file from your terminal: python nba_team_info.py

Expanding the Program (Suggestions):

  • Data Source: Instead of hardcoding the team data, load it from a CSV file, a JSON file, or a database (e.g., SQLite). This makes it easier to update the data without modifying the code.
  • Player Information: Add a section to display key players for the selected team, including their statistics.
  • Season Statistics: Include a tab or section to display the team's season statistics (wins, losses, points per game, etc.). You'll need to find a reliable data source for this information.
  • Images: Display the team logo. You'll need to download the logos and store them in a directory.
  • Web Scraping: If you can't find a suitable API or data source, you could use web scraping to extract data from websites like NBA.com. Be mindful of the website's terms of service and robots.txt file.
  • More Advanced GUI: Use more advanced Tkinter widgets (e.g., tabs, frames, scrollbars) to create a more user-friendly interface.
  • Error Handling: Add error handling to gracefully handle cases where the selected team is not found or the data source is unavailable.

Comments