我用于计算模式的 Tkinter 标签正在返回 "N." 为什么会这样,我该如何解决?
My Tkinter Label for calculating mode is returning "N." Why is this and how do I fix this?
from tkinter import *
import random
from collections import Counter
root = Tk()
root.title("Random")
root.geometry("600x400")
root.resizable(False, False)
def open_saw():
saw_wn = Tk()
saw_wn.title("Random App - Spin a Wheel")
saw_wn.geometry("600x400")
saw_wn.resizable(False, False)
saw_wn.mainloop()
def open_coin():
c_wn = Tk()
c_wn.title("Random App - Flip a Coin")
c_wn.geometry("600x400")
c_wn.resizable(False, False)
Label(c_wn, text=" ").grid(row=0, column=0)
Label(c_wn, text="Flip the coin below!").grid(row=0, column=1)
c_wn.mainloop()
def open_average():
avg_wn = Tk()
avg_wn.title("Random App - Averages")
avg_wn.geometry("800x400")
avg_wn.resizable(False, False)
Label(avg_wn, text=" ").grid(row=0, column=0)
Label(avg_wn, text="Enter your values below to get the averages in mean, median, and mode.(put a space between "
"commas)").grid(row=0, column=1)
Label(avg_wn, text=" ").grid(row=1, column=0)
entry = Entry(avg_wn)
entry.grid(row=2, column=1)
def calculate():
list_data = entry.get().split(', ')
list_data = [float(i) for i in list_data]
mean = sum(list_data) / len(list_data)
Label(avg_wn, text='Mean: ' + str(mean)).grid(row=5, column=0)
list_data_len = len(list_data)
list_data.sort()
if list_data_len % 2 == 0:
median1 = list_data[list_data_len // 2]
median2 = list_data[list_data_len // 2 - 1]
median = (median1 + median2) / 2
else:
median = list_data[list_data_len // 2]
Label(avg_wn, text='Median: ' + str(median)).grid(row=5, column=1)
list_data_for_mode = Counter(list_data)
get_mode = dict(list_data_for_mode)
mode = [k for k, v in get_mode.items() if v == max(list(list_data_for_mode.values()))]
if len(mode) == list_data_len:
get_mode = "No mode found"
else:
get_mode = [str(i) for i in mode]
Label(avg_wn, text=get_mode[0]).grid(row=5, column=2)
Label(avg_wn, text=" ").grid(row=3, column=0)
Button(avg_wn, text='Enter', command=calculate).grid(row=4, column=1)
Label(root, text=" ").grid(row=0, column=0)
title = Label(root, text="Welcome to Random")
title.config(font=("Yu Gothic UI", 24))
title.grid(row=0, column=1)
button1 = Button(root, text=" Spin a wheel ", padx=80, pady=25, command=open_saw)
button1.place(x=2.25, y=100)
button2 = Button(root, text="Calculate mean, mode, median, and range", padx=20, pady=25, command=open_average)
button2.place(x=325, y=100)
button3 = Button(root, text="Flip a Coin", padx=125, pady=25, command=open_coin)
button3.place(x=2.25, y=200)
button4 = Button(root, text="Roll a die", padx=107.5, pady=25)
button4.place(x=325, y=200)
root.mainloop()
我正在制作一个可以解决简单统计问题的应用程序。我目前正在研究计算众数、均值和中位数的方法。均值和中位数正确显示在它们的位置,但众数不是。它显示一个“N”,我不知道它为什么这样做。请告诉我如何修复它,请告诉我它是什么以及它的含义,因为我想从中学习。
是因为当len(mode) == list_data_len
为True
时,get_mode
会被赋值为"No mode found"
。所以 text=get_mode[0]
会将 "N"
分配给标签。 get_mode = ["No mode found"]
应该改用:
def calculate():
...
if len(mode) == list_data_len:
get_mode = ["No mode found"]
else:
get_mode = [str(i) for i in mode]
mode_label.config(text=get_mode[0])
from tkinter import *
import random
from collections import Counter
root = Tk()
root.title("Random")
root.geometry("600x400")
root.resizable(False, False)
def open_saw():
saw_wn = Tk()
saw_wn.title("Random App - Spin a Wheel")
saw_wn.geometry("600x400")
saw_wn.resizable(False, False)
saw_wn.mainloop()
def open_coin():
c_wn = Tk()
c_wn.title("Random App - Flip a Coin")
c_wn.geometry("600x400")
c_wn.resizable(False, False)
Label(c_wn, text=" ").grid(row=0, column=0)
Label(c_wn, text="Flip the coin below!").grid(row=0, column=1)
c_wn.mainloop()
def open_average():
avg_wn = Tk()
avg_wn.title("Random App - Averages")
avg_wn.geometry("800x400")
avg_wn.resizable(False, False)
Label(avg_wn, text=" ").grid(row=0, column=0)
Label(avg_wn, text="Enter your values below to get the averages in mean, median, and mode.(put a space between "
"commas)").grid(row=0, column=1)
Label(avg_wn, text=" ").grid(row=1, column=0)
entry = Entry(avg_wn)
entry.grid(row=2, column=1)
def calculate():
list_data = entry.get().split(', ')
list_data = [float(i) for i in list_data]
mean = sum(list_data) / len(list_data)
Label(avg_wn, text='Mean: ' + str(mean)).grid(row=5, column=0)
list_data_len = len(list_data)
list_data.sort()
if list_data_len % 2 == 0:
median1 = list_data[list_data_len // 2]
median2 = list_data[list_data_len // 2 - 1]
median = (median1 + median2) / 2
else:
median = list_data[list_data_len // 2]
Label(avg_wn, text='Median: ' + str(median)).grid(row=5, column=1)
list_data_for_mode = Counter(list_data)
get_mode = dict(list_data_for_mode)
mode = [k for k, v in get_mode.items() if v == max(list(list_data_for_mode.values()))]
if len(mode) == list_data_len:
get_mode = "No mode found"
else:
get_mode = [str(i) for i in mode]
Label(avg_wn, text=get_mode[0]).grid(row=5, column=2)
Label(avg_wn, text=" ").grid(row=3, column=0)
Button(avg_wn, text='Enter', command=calculate).grid(row=4, column=1)
Label(root, text=" ").grid(row=0, column=0)
title = Label(root, text="Welcome to Random")
title.config(font=("Yu Gothic UI", 24))
title.grid(row=0, column=1)
button1 = Button(root, text=" Spin a wheel ", padx=80, pady=25, command=open_saw)
button1.place(x=2.25, y=100)
button2 = Button(root, text="Calculate mean, mode, median, and range", padx=20, pady=25, command=open_average)
button2.place(x=325, y=100)
button3 = Button(root, text="Flip a Coin", padx=125, pady=25, command=open_coin)
button3.place(x=2.25, y=200)
button4 = Button(root, text="Roll a die", padx=107.5, pady=25)
button4.place(x=325, y=200)
root.mainloop()
我正在制作一个可以解决简单统计问题的应用程序。我目前正在研究计算众数、均值和中位数的方法。均值和中位数正确显示在它们的位置,但众数不是。它显示一个“N”,我不知道它为什么这样做。请告诉我如何修复它,请告诉我它是什么以及它的含义,因为我想从中学习。
是因为当len(mode) == list_data_len
为True
时,get_mode
会被赋值为"No mode found"
。所以 text=get_mode[0]
会将 "N"
分配给标签。 get_mode = ["No mode found"]
应该改用:
def calculate():
...
if len(mode) == list_data_len:
get_mode = ["No mode found"]
else:
get_mode = [str(i) for i in mode]
mode_label.config(text=get_mode[0])