Track Olympic Medals with PySimpleGUI! 🥇🥳

With the Olympics in Paris trending right now, let’s jump on the bandwagon and create a fun and interactive GUI app that lets users choose their favorite sports from a drop-down list and displays the gold medalists for the past three Olympics.

This tutorial will make the learning process enjoyable and engaging!

 

Step 1: Set Up Your Environment

Before we start, make sure you have PySimpleGUI installed. You can install it using pip if you haven't already:

pip install pysimplegui

 

Step 2: Import the Necessary Libraries

Let's start by importing the required libraries.

 

import PySimpleGUI as sg

 

Step 3: Create the GUI Layout

We’ll create a simple layout with a drop-down menu for selecting a sport and a text element to display the results.

 

sports = ['Athletics', 'Swimming', 'Gymnastics', 'Cycling', 'Rowing', 'Boxing', 'Judo', 'Wrestling', 'Basketball', 'Soccer']

 

layout = [

    [sg.Text('Select Your Favorite Sport:')],

    [sg.Combo(sports, key='-SPORT-', readonly=True)],

    [sg.Button('Show Gold Medalists')],

    [sg.Text('Gold Medalists:', size=(40, 5), key='-RESULT-')]

]

 

window = sg.Window('Olympics Medal Tracker', layout)

 

Step 4: Define the Medalists Data

For simplicity, we’ll use a dictionary to store the gold medalists for each sport for the past three Olympics.

 

medalists = {

    'Athletics': ['Usain Bolt (2012)', 'Mo Farah (2016)', 'Eliud Kipchoge (2020)'],

    'Swimming': ['Michael Phelps (2012)', 'Katie Ledecky (2016)', 'Caeleb Dressel (2020)'],

    'Gymnastics': ['Gabby Douglas (2012)', 'Simone Biles (2016)', 'Sunisa Lee (2020)'],

    'Cycling': ['Bradley Wiggins (2012)', 'Chris Froome (2016)', 'Richard Carapaz (2020)'],

    'Rowing': ['Katherine Grainger (2012)', 'Helen Glover (2016)', 'Stefanos Ntouskos (2020)'],

    'Boxing': ['Anthony Joshua (2012)', 'Claressa Shields (2016)', 'Arlen López (2020)'],

    'Judo': ['Kayla Harrison (2012)', 'Majlinda Kelmendi (2016)', 'Shohei Ono (2020)'],

    'Wrestling': ['Jordan Burroughs (2012)', 'Helen Maroulis (2016)', 'Gable Steveson (2020)'],

    'Basketball': ['LeBron James (2012)', 'Kevin Durant (2016)', 'Kevin Durant (2020)'],

    'Soccer': ['Neymar (2012)', 'Neymar (2016)', 'Malcolm (2020)']

}

Layout with Text, Dropdown List and Buttons

Step 5: Add Event Handling

We’ll add an event loop to handle user interactions. When the user selects a sport and clicks the button, the app will display the corresponding gold medalists.

 

while True:

    event, values = window.read()

 

    if event == sg.WIN_CLOSED:

        break

 

    if event == 'Show Gold Medalists':

        sport = values['-SPORT-']

        if sport:

            results = '\n'.join(medalists[sport])

            window['-RESULT-'].update(f'Gold Medalists:\n{results}')

        else:

            window['-RESULT-'].update('Please select a sport.')

 

window.close()

 

Step 6: Run the App

 

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

 

import PySimpleGUI as sg

 

sports = ['Athletics', 'Swimming', 'Gymnastics', 'Cycling', 'Rowing', 'Boxing', 'Judo', 'Wrestling', 'Basketball', 'Soccer']

 

layout = [

    [sg.Text('Select Your Favorite Sport:')],

    [sg.Combo(sports, key='-SPORT-', readonly=True)],

    [sg.Button('Show Gold Medalists')],

    [sg.Text('Gold Medalists:', size=(40, 5), key='-RESULT-')]

]

 

window = sg.Window('Olympics Medal Tracker', layout)

 

medalists = {

    'Athletics': ['Usain Bolt (2012)', 'Mo Farah (2016)', 'Eliud Kipchoge (2020)'],

    'Swimming': ['Michael Phelps (2012)', 'Katie Ledecky (2016)', 'Caeleb Dressel (2020)'],

    'Gymnastics': ['Gabby Douglas (2012)', 'Simone Biles (2016)', 'Sunisa Lee (2020)'],

    'Cycling': ['Bradley Wiggins (2012)', 'Chris Froome (2016)', 'Richard Carapaz (2020)'],

    'Rowing': ['Katherine Grainger (2012)', 'Helen Glover (2016)', 'Stefanos Ntouskos (2020)'],

    'Boxing': ['Anthony Joshua (2012)', 'Claressa Shields (2016)', 'Arlen López (2020)'],

    'Judo': ['Kayla Harrison (2012)', 'Majlinda Kelmendi (2016)', 'Shohei Ono (2020)'],

    'Wrestling': ['Jordan Burroughs (2012)', 'Helen Maroulis (2016)', 'Gable Steveson (2020)'],

    'Basketball': ['LeBron James (2012)', 'Kevin Durant (2016)', 'Kevin Durant (2020)'],

    'Soccer': ['Neymar (2012)', 'Neymar (2016)', 'Malcolm (2020)']

}

 

while True:

    event, values = window.read()

 

    if event == sg.WIN_CLOSED:

        break

 

    if event == 'Show Gold Medalists':

        sport = values['-SPORT-']

        if sport:

            results = '\n'.join(medalists[sport])

            window['-RESULT-'].update(f'Gold Medalists:\n{results}')

        else:

            window['-RESULT-'].update('Please select a sport.')

 

window.close()

 

Output

Sports - Gymnastics

 

Gold Medalists

Let’s Sum it Up!

In this post, you've learned how to create your very own Olympics Medal Tracker using PySimpleGUI! 🥇🎉 We walked through building a fun and interactive app where users can select their favorite sports from a dropdown list and see the gold medalists for the past three Olympics. Along the way, you learned how to set up the GUI, handle user input, and display data.

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 explore how to create a fun and interactive leaderboard for the current Olympics. Stay tuned and keep coding 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.