Python offers multiple options for developing a GUI (Graphical User Interface). Out of all the GUI methods, Tkinter is the most commonly used method. In this article, we will learn how to create a Rank Based – Percentile Gui Calculator application using Tkinter, with a step-by-step guide.
Prerequisites:
To create a Tkinter:
- Importing the module – Tkinter
- Create the main window (container)
- Add any number of widgets to the main window.
- Apply the event Trigger on the widgets.
The GUI would look like below:
Let’s create a GUI-based simple Rank Based – Percentile Gui Calculator application:
Below is the implementation:
Python3
# import all functions from the tkinter from tkinter import *
# Function to calculate the percentile def getPercentile() :
# take a value from the respective entry boxes
# get method returns current text as string
students = int (total_participantField.get())
rank = int (rankField.get())
# variable to store the result upto 3
# decimal points
result = round ((students - rank) / students * 100 , 3 );
# insert method inserting the
# value in the text entry box.
percentileField.insert( 10 , str (result))
# Function for clearing the # contents of all text entry boxes def Clear():
# deleting the content from the entry box
rankField.delete( 0 , END)
total_participantField.delete( 0 , END)
percentileField.delete( 0 , END)
# Driver Code if __name__ = = "__main__" :
# Create a GUI window
gui = Tk()
# Set the background colour of GUI window
gui.configure(background = "light green" )
# set the name of tkinter GUI window
gui.title( "Rank Based- Percentile Calculator" )
# Set the configuration of GUI window
gui.geometry( "650x200" )
# Create a Rank: label
rank = Label(gui, text = "Rank" , bg = "blue" )
# Create a And: label
andl = Label(gui, text = "And" , bg = "blue" )
# Create a Total Participants : label
total_participant = Label(gui,
text = "Total Participants" ,
bg = "blue" )
# Create a Find Percentile Button and
# attached to getPercentile function
find = Button(gui, text = "Find Percentile" ,
fg = "Black" , bg = "Red" ,
command = getPercentile)
# Create a Percentile : label
percentile = Label(gui, text = "Percentile" , bg = "blue" )
# Create a Clear Button and attached
# to Clear function
clear = Button(gui, text = "Clear" ,
fg = "Black" , bg = "Red" ,
command = Clear)
# grid method is used for placing
# the widgets at respective positions
# in table like structure .
# padx attributed provide x-axis margin
# from the root window to the widget.
rank.grid(row = 1 , column = 1 ,padx = 10 )
andl.grid(row = 1 , column = 4 )
total_participant.grid(row = 1 , column = 6 , padx = 10 )
# pady attributed provide y-axis
# margin from the widget.
find.grid(row = 3 , column = 4 ,pady = 10 )
percentile.grid(row = 4 , column = 3 ,padx = 10 )
clear.grid(row = 5 , column = 4 ,pady = 10 )
# Create a text entry box for filling or
# typing the information.
rankField = Entry(gui)
total_participantField = Entry(gui)
percentileField = Entry(gui)
# grid method is used for placing
# the widgets at respective positions
# in table like structure .
rankField.grid(row = 1 , column = 2 )
total_participantField.grid(row = 1 , column = 7 )
percentileField.grid(row = 4 , column = 4 )
# Start the GUI
gui.mainloop()
|
Output :
Code Explanation:
- The code starts by getting the number of students and the rank of those students.
- The code then creates two variables, students and rank.
- The getPercentile() function will take these numbers as input and calculate their percentile according to a set formula.
- The getPercentile() function first takes the value from the total_participantField box.
- This is the total number of participants in the study.
- Next, it gets the value from the rankField box.
- This is how many people have a particular rank in this study.
- Finally, getPercentile() calculates each student’s percentile according to this set formula: where x is equal to the value from total_participantField, y is equal to the value from rankField, and z is equal to 100%.
- The code calculates the percentile for a given set of values.
- The first step is to get the number of students and rank.
- Next, the code calculates the median and mode for these two values.
- The getPercentile() function then takes these two values as input and stores them in a new variable called students and rank respectively.
- Next, the percentile is calculated using the following equation: (students/total_participantField.get()) * 100
- The code starts by creating a window called “gui”.
- The window has a green background color.
- Next, the code sets up some basic properties of the window.
- It specifies that the title of the window is “Analyze” and that it should be displayed in a floating panel on the left side of the screen.
- The next line of code creates an instance of Tkinter, which is Python’s built-in GUI library.
- Next, the code sets up some basic properties for the window.
- It specifies that it should have a border around it and that its title bar should be hidden.
- Finally, the code assigns values to two variables: rankField and total_participantField.
- rankField stores information about students’ ranks while total_participantField stores information about all participants in the study (i.e., both students and non-students).
- Next, the code calculates student ranks using round() function and inserts them into rankField variable.
- Then, it calculates participant counts using count() function and inserts them into total_participantField variable.
- Finally, it uses percentile() function to calculate percentage distributions for each rank group (i.e., 1st through 10th grades).
- The code creates a window and sets the background color to light green.
- Next, it sets the name of the window to “Comprehend”.
- Finally, it creates an instance of Tkinter and begins setting up its various properties.
- The next block of code creates two text entry boxes – one for the rank field and one for the total_participant field.
- The code then calculates the percentage of students who are lower than the student’s rank and stores this value in the percentileField text entry box.
- The last block of code clears all of the text entry boxes by deleting their contents.
- The code creates three labels: rank, andl, and total_participant.
- The rank label has the text “Rank” and is blue.
- The andl label has the text “And” and is blue.
- The total_participant label has the text “Total Participants” and is blue.
- The code sets up the GUI window with a geometry of 650×200 pixels.
- It creates two Label objects, one for the rank label and one for the andl label.
- The rank object has the text “Rank” as its value, while the andl object has the text “And” as its value.
- Finally, the code creates a Total Participant object with the text “Total Participants” as its value.
- The code creates a Rank: label, an And: label, and a Total Participants: label.
- The Rank: label will display the rank of the data set, while the And: label will display whether all data sets have an equal number of participants or not.
- The Total Participants: label will display the total number of participants in each data set.
- The code starts with the creation of three widgets: a find percentile button, a percentile label, and a clear button.
- The find percentile button is attached to the getPercentile function.
- This function takes two arguments: the dataframe and the percentile rank.
- The code creates an instance of the Button class and sets its properties accordingly.
- The fg property specifies the foreground color, while bg specifies the background color.
- The command property specifies how to execute the function when clicked.
- The percentile label is created next and attached to the getPercentile function as well.
- Like the find percentile button, it also has a command property that allows you to specify how to execute it when clicked.
- Finally, we create a grid method that will help us place all of our widgets in table-like structure onscreen.
- We use this grid method to position both buttons at 0% (the first column) and 100% (the last column).
- The code creates a Find Percentile button and attached to it is a getPercentile function.
- The find widget will display the text “Find Percentile” and have a black background.
- The percentile label will also be created and have a blue background.
- Finally, two buttons are created, Clear and Percentile.
- Clear will have a black background and have the command “Clear” associated with it, while Percentile will have a red background and the command “Get Percentile” associated with it.
- The code then uses the grid method to place the widgets at respective positions in table like structure .
- This allows for easy positioning of all of the widgets on the screen.