➡️Build Your Fun Olympics Leaderboard Now! 🏅🚀

 

Continuing our Olympic-themed projects, let’s dive into creating a fun and interactive leaderboard for the current Olympics using PySimpleGUI.

This project will allow users to view and update the standings, making it an engaging way to track the progress of your favourite athletes and countries.

Let’s get started!

 

Step 1: Set Up Your Environment

First, ensure you have PySimpleGUI installed. If you haven’t installed it yet, use the following command:

pip install pysimplegui

 

Step 2: Import the Required Libraries

Begin by importing the necessary libraries.

import PySimpleGUI as sg

 

Step 3: Create the GUI Layout

We'll create a simple layout that includes a table to display the leaderboard, and inputs to add or update entries.

 

layout = [

    [sg.Text('Olympics Leaderboard', font=('Helvetica', 16))],

    [sg.Table(

        values=[],

        headings=['Country', 'Gold', 'Silver', 'Bronze'],

        max_col_width=25,

        auto_size_columns=True,

        display_row_numbers=False,

        justification='right',

        num_rows=10,

        key='-TABLE-',

        row_height=25)],

    [sg.Text('Country:'), sg.InputText(key='-COUNTRY-')],

    [sg.Text('Gold:'), sg.InputText(key='-GOLD-')],

    [sg.Text('Silver:'), sg.InputText(key='-SILVER-')],

    [sg.Text('Bronze:'), sg.InputText(key='-BRONZE-')],

    [sg.Button('Add/Update'), sg.Button('Exit')]

]

 

window = sg.Window('Olympics Leaderboard', layout)

The Layout

Step 4: Initialize Leaderboard Data

We'll create a dictionary to store the leaderboard data. Each country will have a list of medal counts.

leaderboard = {}

 

Step 5: Add Event Handling

We'll add an event loop to handle user interactions. The loop will handle adding and updating entries in the leaderboard.

 

while True:

    event, values = window.read()

 

    if event sg.WIN_CLOSED or event 'Exit':

        break

 

    if event == 'Add/Update':

        country = values['-COUNTRY-']

        gold = int(values['-GOLD-'])

        silver = int(values['-SILVER-'])

        bronze = int(values['-BRONZE-'])

 

        if country:

            leaderboard[country] = [gold, silver, bronze]

            data = [[country] + leaderboard[country] for country in leaderboard]

            window['-TABLE-'].update(values=data)

 

window.close()

 

Step 6: Run the App

Put everything together and run your app. Here’s the complete code:

 

import PySimpleGUI as sg

 

layout = [

    [sg.Text('Olympics Leaderboard', font=('Helvetica', 16))],

    [sg.Table(

        values=[],

        headings=['Country', 'Gold', 'Silver', 'Bronze'],

        max_col_width=25,

        auto_size_columns=True,

        display_row_numbers=False,

        justification='right',

        num_rows=10,

        key='-TABLE-',

        row_height=25)],

    [sg.Text('Country:'), sg.InputText(key='-COUNTRY-')],

    [sg.Text('Gold:'), sg.InputText(key='-GOLD-')],

    [sg.Text('Silver:'), sg.InputText(key='-SILVER-')],

    [sg.Text('Bronze:'), sg.InputText(key='-BRONZE-')],

    [sg.Button('Add/Update'), sg.Button('Exit')]

]

 

window = sg.Window('Olympics Leaderboard', layout)

 

leaderboard = {}

 

while True:

    event, values = window.read()

 

    if event sg.WIN_CLOSED or event 'Exit':

        break

 

    if event == 'Add/Update':

        country = values['-COUNTRY-']

        gold = int(values['-GOLD-'])

        silver = int(values['-SILVER-'])

        bronze = int(values['-BRONZE-'])

 

        if country:

            leaderboard[country] = [gold, silver, bronze]

            data = [[country] + leaderboard[country] for country in leaderboard]

            window['-TABLE-'].update(values=data)

 

window.close()

Output

Let’s Sum it Up!

In this post, you’ve learned how to create a fun and interactive Olympics Leaderboard using PySimpleGUI! 🏅📊

This project combined essential programming concepts with practical GUI development, showing you how to build a functional and engaging application with PySimpleGUI.

Great job, and get ready for more exciting challenges ahead!

Let’s Inspire Future AI Coders Together! ☕

 

I’m excited to continue sharing my passion for Python programming and AI with you all. If you’ve enjoyed the content and found it helpful, do consider supporting my work with a small gift. Just click the link below to make a difference – it’s quick, easy, and every bit helps and motivates me to keep creating awesome contents for you.

Thank you for being amazing!

What’s Next? 📅

In our next post, we’ll dive into customizing the appearance of your leaderboard with PySimpleGUI themes. You’ll learn how to make your app not only functional but also visually appealing. Stay tuned and keep having fun with PySimpleGUI! 🎨✨

Ready for More Python Fun? 📬

Subscribe to our newsletter now and get a free Python cheat sheet! 📑 Dive deeper into Python programming with more exciting projects and tutorials designed just for beginners.

Keep learning, keep coding 👩‍💻👨‍💻, and keep discovering new possibilities! 💻

Enjoy your journey into artificial intelligence, machine learning, data analytics, data science and more with Python!

Stay tuned for our next exciting project in the following edition!

Happy coding!🚀📊✨

🎉 We want to hear from you! 🎉 How do you feel about our latest newsletter? Your feedback will help us make it even more awesome!

Login or Subscribe to participate in polls.