Search code examples
pythontkinterbuttoneventsaverage

Python Tkinter finding the average of star rating user button click


I am really new to python and Tkinter but I am trying to make
this GUI.

It is essentially a star rating system that contains 5 buttons from 1-5. The user has to click the rating button they would like to enter and it should average out the star rating based on the previous button clicks.

This is the code I have so far but I'm stuck with how to make the average from the button click events.

Any help would be very much appreciated.

    from tkinter import *
    ws = Tk()
    ws.title("Cleanliness Rater")
    ws.geometry("500x500")

Label(ws, text="Please rate the cleanliness of this bus: ", font=("arial ", 12) ).pack()


Label(ws, text="):", font=("arial ", 12) ).pack()

one = Button(ws, text="1", borderwidth=3, relief="raised", padx=5, pady=10).pack(padx=5, pady=10)

two = Button(ws, text="2", borderwidth=3, relief="raised", padx=5, pady=10).pack(padx=5, pady=10)

three = Button(ws, text="3", borderwidth=3, relief="raised", padx=5, pady=10).pack(padx=5, pady=10)

four = Button(ws, text="4", borderwidth=3, relief="raised", padx=5, pady=10).pack(padx=5, pady=10)

five = Button(ws, text="5", borderwidth=3, relief="raised", padx=5, pady=10).pack(padx=5, pady=10)

Label(ws, text="(:", font=("arial ", 12) ).pack()


ws.mainloop()

Solution

  • You need to use StringVar() (here's a article about it) and functions or lambdas to do this.

    First we'll keep a track of sum of all total rating in a variable named count and total number of increments in a variable named increments

    Calculating Average rating from here would be fairly easy, it'd be count/increments

    Then we'll create a StringVar() to keep track of the label text since it'd be dynamic. We'll initially set it to "Unrated".

    count = 0
    ans = StringVar(ws)
    ans.set("Unrated")
    increments = 0
    

    Then we'll have a function that increments the count variable and the increment counter for us, it'd also change the label text, the StringVar.

    def increment_count(x):
        global count,increments,ans
        count += x
        increments+=1
        ans.set(str(count/increments))
    

    Assign the commands to each buttons.

    Button(ws, ... ,command = lambda:increment_count(1)).pack(padx=5, pady=10)
    

    And we're done!

    from tkinter import *
    ws = Tk()
    ws.title("Cleanliness Rater")
    ws.geometry("500x500")
    
    Label(ws, text="Please rate the cleanliness of this bus: ", font=("arial ", 12) ).pack()
    
    
    Label(ws, text="):", font=("arial ", 12) ).pack()
    
    
    count = 0
    ans = StringVar(ws)
    ans.set("Unrated")
    increments = 0
    
    def increment_count(x):
        global count,increments,ans
        count += x
        increments+=1
        ans.set(str(count/increments))
    
    one = Button(ws, text="1", borderwidth=3, relief="raised", padx=5, pady=10,command = lambda:increment_count(1)).pack(padx=5, pady=10)
    
    two = Button(ws, text="2", borderwidth=3, relief="raised", padx=5, pady=10,command=lambda:increment_count(2)).pack(padx=5, pady=10)
    
    three = Button(ws, text="3", borderwidth=3, relief="raised", padx=5, pady=10,command=lambda:increment_count(3)).pack(padx=5, pady=10)
    
    four = Button(ws, text="4", borderwidth=3, relief="raised", padx=5, pady=10,command=lambda:increment_count(4)).pack(padx=5, pady=10)
    
    five = Button(ws, text="5", borderwidth=3, relief="raised", padx=5, pady=10,command=lambda:increment_count(5)).pack(padx=5, pady=10)
    
    Label(ws, text="(:", font=("arial ", 12) ).pack()
    
    Label(ws,textvariable=ans).pack()
    ws.mainloop()
    

    enter image description here