Study With Me: Building a Simple GUI with PySimpleGUI as a Side Project

Posted by


In this tutorial, we will create a simple study with me application using PySimpleGUI, a Python GUI library. This application will allow users to set a timer for their study session and take breaks in between. This project is perfect for anyone looking to improve their productivity and focus during study or work sessions.

Step 1: Install PySimpleGUI
To begin, make sure you have PySimpleGUI installed on your system. You can install it using pip by running the following command in your terminal or command prompt:

pip install PySimpleGUI

Step 2: Import necessary modules
Next, we will import the necessary modules for our project. We will need PySimpleGUI for creating the GUI and the time module for handling time-related functionalities. Create a new Python script and import the following modules:

import PySimpleGUI as sg
import time

Step 3: Create the GUI layout
We will now create the layout for our study with me application. We will use PySimpleGUI’s layout syntax to create a simple and intuitive user interface. Here’s a basic example layout:

layout = [
    [sg.Text("Study Timer", size=(20, 1), font=("Helvetica", 25))],
    [sg.Text("Set study time (minutes):"), sg.InputText(size=(10, 1), key='-study_time-')],
    [sg.Text("Set break time (minutes):"), sg.InputText(size=(10, 1), key='-break_time-')],
    [sg.Button("Start"), sg.Button("Reset"), sg.Button("Exit")],
    [sg.Text("", size=(20, 1), font=("Helvetica", 25), key='-timer-')]
]

Step 4: Create the window and event loop
Now let’s create a window using the layout we defined in the previous step and set up an event loop to handle user interactions. We will also define functions for starting and resetting the timer:

window = sg.Window("Study with me", layout)

def start_timer(study_time, break_time):
    total_time = study_time * 60
    start_time = time.time()

    while total_time >= 0:
        mins, secs = divmod(total_time, 60)
        time_str = '{:02d}:{:02d}'.format(mins, secs)
        window['-timer-'].update(value=time_str)
        event, values = window.read(timeout=1000)

        if event == "Reset" or event == sg.WIN_CLOSED:
            break

        total_time -= 1

        if total_time == 0:
            sg.popup("Time is up!", title="Study with me")
            time.sleep(break_time * 60)
            total_time = study_time * 60

def reset_timer():
    window['-timer-'].update(value='00:00')

while True:
    event, values = window.read()

    if event == "Start":
        study_time = int(values['-study_time-'])
        break_time = int(values['-break_time-'])
        start_timer(study_time, break_time)

    if event == "Reset":
        reset_timer()

    if event == "Exit" or event == sg.WIN_CLOSED:
        break

window.close()

Step 5: Run the application
Save the script and run it in your terminal or command prompt. You should see a window with input fields for study time and break time, as well as buttons to start, reset, and exit the timer. You can set the study and break times, click start to begin the timer, and take breaks in between study sessions.

That’s it! You have successfully created a study with me application using PySimpleGUI. Feel free to customize the layout and functionality to suit your needs. Happy studying!

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x