Python Tkinter 寻找星级评分用户按钮点击的平均值

Python Tkinter finding the average of star rating user button click

我是 python 和 Tkinter 的新手,但我正在努力
this GUI.

它本质上是一个星级评分系统,包含 1-5 中的 5 个按钮。用户必须点击他们想要输入的评级按钮,它应该根据之前的按钮点击平均出星级。

这是我目前拥有的代码,但我仍然无法理解如何从按钮点击事件中获取平均值。

非常感谢任何帮助。

    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()

你需要有与你的按钮关联的命令,我使用 partial 将参数传递给函数。 如果运行正常,请更新我。

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()

您需要使用 StringVar()here's a article about it) and functions or lambdas 来执行此操作。

首先,我们将跟踪名为 count 的变量中所有总评分的总和以及名为 increments

的变量中的增量总数

从这里计算平均评分相当容易,它是 count/increments

然后我们将创建一个 StringVar() 来跟踪标签文本,因为它是动态的。我们最初将其设置为“未评级”。

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

然后我们将有一个函数为我们递增计数变量和递增计数器,它还会更改标签文本 StringVar。

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

将命令分配给每个按钮。

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

大功告成!

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()