Home > Enterprise >  Python Tkinter finding the average of star rating user button click
Python Tkinter finding the average of star rating user button click

Time:05-29

I am really new to python and Tkinter but I am trying to make
enter image description here

CodePudding user response:

you need to have command associated with your button, i used partial to pass an argument to the function. Update me if this runs fine.

from tkinter import *
from functools import partial
ws = Tk()
ws.title("Cleanliness Rater")
ws.geometry("500x500")
score = []



total_rating = 0
def calculate(x):
    
    global score 
    score.append(x)
    total_rating = sum(score)/len(score)
    print(total_rating)
    #Label2(ws, text="Please rate the cleanliness rating till now: " total_rating, font=("arial ", 12) ).pack()
    
Label(ws, text="Please rate the cleanliness of this bus: ", font=("arial ", 12) ).pack()


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

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

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

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

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

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

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


ws.mainloop()
  • Related