Open In App

Rank Based Percentile Gui Calculator using Tkinter

Last Updated : 11 Dec, 2022
Like Article

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.


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:


# 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
    # 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

Output : 

Code Explanation:

  1. The code starts by getting the number of students and the rank of those students.
  2. The code then creates two variables, students and rank.
  3. The getPercentile() function will take these numbers as input and calculate their percentile according to a set formula.
  4. The getPercentile() function first takes the value from the total_participantField box.
  5. This is the total number of participants in the study.
  6. Next, it gets the value from the rankField box.
  7. This is how many people have a particular rank in this study.
  8. 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%.
  9. The code calculates the percentile for a given set of values.
  10. The first step is to get the number of students and rank.
  11. Next, the code calculates the median and mode for these two values.
  12. The getPercentile() function then takes these two values as input and stores them in a new variable called students and rank respectively.
  13. Next, the percentile is calculated using the following equation: (students/total_participantField.get()) * 100
  14. The code starts by creating a window called “gui”.
  15. The window has a green background color.
  16. Next, the code sets up some basic properties of the window.
  17. 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.
  18. The next line of code creates an instance of Tkinter, which is Python’s built-in GUI library.
  19. Next, the code sets up some basic properties for the window.
  20. It specifies that it should have a border around it and that its title bar should be hidden.
  21. Finally, the code assigns values to two variables: rankField and total_participantField.
  22. rankField stores information about students’ ranks while total_participantField stores information about all participants in the study (i.e., both students and non-students).
  23. Next, the code calculates student ranks using round() function and inserts them into rankField variable.
  24. Then, it calculates participant counts using count() function and inserts them into total_participantField variable.
  25. Finally, it uses percentile() function to calculate percentage distributions for each rank group (i.e., 1st through 10th grades).
  26. The code creates a window and sets the background color to light green.
  27. Next, it sets the name of the window to “Comprehend”.
  28. Finally, it creates an instance of Tkinter and begins setting up its various properties.
  29. The next block of code creates two text entry boxes – one for the rank field and one for the total_participant field.
  30. 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.
  31. The last block of code clears all of the text entry boxes by deleting their contents.
  32. The code creates three labels: rank, andl, and total_participant.
  33. The rank label has the text “Rank” and is blue.
  34. The andl label has the text “And” and is blue.
  35. The total_participant label has the text “Total Participants” and is blue.
  36. The code sets up the GUI window with a geometry of 650×200 pixels.
  37. It creates two Label objects, one for the rank label and one for the andl label.
  38. The rank object has the text “Rank” as its value, while the andl object has the text “And” as its value.
  39. Finally, the code creates a Total Participant object with the text “Total Participants” as its value.
  40. The code creates a Rank: label, an And: label, and a Total Participants: label.
  41. 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.
  42. The Total Participants: label will display the total number of participants in each data set.
  43. The code starts with the creation of three widgets: a find percentile button, a percentile label, and a clear button.
  44. The find percentile button is attached to the getPercentile function.
  45. This function takes two arguments: the dataframe and the percentile rank.
  46. The code creates an instance of the Button class and sets its properties accordingly.
  47. The fg property specifies the foreground color, while bg specifies the background color.
  48. The command property specifies how to execute the function when clicked.
  49. The percentile label is created next and attached to the getPercentile function as well.
  50. Like the find percentile button, it also has a command property that allows you to specify how to execute it when clicked.
  51. Finally, we create a grid method that will help us place all of our widgets in table-like structure onscreen.
  52. We use this grid method to position both buttons at 0% (the first column) and 100% (the last column).
  53. The code creates a Find Percentile button and attached to it is a getPercentile function.
  54. The find widget will display the text “Find Percentile” and have a black background.
  55. The percentile label will also be created and have a blue background.
  56. Finally, two buttons are created, Clear and Percentile.
  57. 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.
  58. The code then uses the grid method to place the widgets at respective positions in table like structure .
  59. This allows for easy positioning of all of the widgets on the screen.

Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads