Creating my First GUI Application using Python


We will learn

Creating GUI Window in python: Tkinter

Using nested loops

Playing sound in python


How this program works ( General Logic )––––


Problems Faced–––

I wanted a GUI program, so I used Tkinter, but the timer variable(stime) was not updating causing no output in the GUI window.

So, I used the update() function in Tkinter to update ‘stime’, every time I set the value to stime in the loop.


Program:
––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
from tkinter import *
import time
import playsound


self = Tk()
# setting the title for our window
self.title("Excercise Timer")

#configuring background colour for our window
self.configure(bg="black")

#creating a variable basically this is our timer label
stime = StringVar()

#setting the initial value of the variable
stime.set("00")

#function
def func():

    nsets = int(setsVar.get()) #getting values from the sets entry
    wt = int(wtimeVar.get())# getting valuers from workout time entry
    rt = int(rtimeVar.get())# getting values from the relax time entry

    for count in range(nsets):

        playsound.playsound("start.mp3")  # using play sound module to play the sound from selected file

        for count in range(1, wt + 1):  # this is a loop of range workout time

            t = (wt - count)  # initializing a  local variable for the value of the second
            stime.set(t)  # setting the value 't' to our timer label
            self.update()  # updatind tkinter(GUI window) so that the value changes and updates in every loop 
            time.sleep(1) # this will wait for 1 sec and continues the loop 

        playsound.playsound("relax.mp3")# using play sound module to play the sound from selected file

        for count in range(1, rt + 1):# this is a loop of range relax time

            r = (rt - count) # initializing a  local variable for the value of the second
            stime.set(r) # setting the value 'r' to our timer label
            self.update() # updatind tkinter(GUI window) so that the value changes and updates in every loop 
            time.sleep(1) # this will wait for 1 sec and continues the loop

    playsound.playsound("congrats.mp3")# using play sound module to play the sound from selected file




# Now here we are setting our GUI window structure
#no.of exercises
 # This will create a label named NO.OF Excersices

setsLabel = Label(self, text = "NO.OF Exercises" , bg='#000', fg='#fff') setsLabel.pack()
setsVar = DoubleVar() # creating a variable
# creating a entry for the label above and assingnig the variable to store the input
setsEntry = Entry(self, textvariable = setsVar, bg='#000', fg='#fff', justify='center') 
setsEntry.pack()

#Workouts
# This will create a label named Workout Time(seconds)
wtimeLabel = Label(self, text = "Workout Time(seconds)", bg='#000', fg='#fff')
wtimeLabel.pack()
wtimeVar = DoubleVar() # creating a variable 
# creating a entry for the label above and assingnig the variable to store the input
wtimeEntry = Entry(self, textvariable = wtimeVar, bg='#000', fg='#fff', justify='center')
wtimeEntry.pack()
# setting the deafault value of variable ; note since entry is assigned with this variable this reflects on the window
wtimeVar.set(45)

#relax time
# This will create a label named Relax Time(seconds)
rtimeLabel = Label(self, text="Relax time(seconds)",bg='#000', fg='#fff', )
rtimeLabel.pack()
rtimeVar = DoubleVar()
# creating a entry for the label above and assingnig the variable to store the input

rtimeEntry = Entry(self, textvariable=rtimeVar, bg='#000', fg='#fff', justify='center')
rtimeEntry.pack()
# setting the deafault value of variable ; note since entry is assigned with this variable this reflects on the window

rtimeVar.set(20)

#button
# setting up a button and assigning a function to perform when clicked.
b = Button(self, text = "START", command =func, bg='#000', fg='#000', pady=20, padx=5 )
b.pack()


# Timer Label
# note we have assigned the variable in the start of the script 
# this is the label for our timer
stimeLabel = Label(self, textvariable=stime, font=("Arial", 90), bg='#000', fg='#fff')
# setting the height, width, padding in x and y directions of the label.
stimeLabel.pack(padx=90, pady = 40)


self.mainloop()# calling the program in  a loop, functions untill the window is closed

––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––

OUTPUT–––––––––––


while running

Default output(when opened)


NOTE::
to run the program successfully on your computer
You should have python installed and have the Timer.py file and the alarm file on the same folder .
change the name of the sound to your alarm file .




Comments

Popular posts from this blog