Python 为什么在tkinter中查找中值时会出现错误?

Python 为什么在tkinter中查找中值时会出现错误?,python,tkinter,statistics,median,Python,Tkinter,Statistics,Median,我正在使用tkinter编写一个程序,以查找平均值或中位数(取决于用户选择哪一个)。在查找平均值时,中位数仅在我打印值(而不是配置文本)时起作用 我评估了(使用eval())这些值以找到中间值,但它给出了以下误差: TypeError:只能将str(而不是int)连接到str。 以下是我代码的一小部分: from tkinter import * import statistics root = Tk() class stats: def __init__(self, const

我正在使用tkinter编写一个程序,以查找平均值或中位数(取决于用户选择哪一个)。在查找平均值时,中位数仅在我打印值(而不是配置文本)时起作用

我评估了(使用
eval()
)这些值以找到中间值,但它给出了以下误差:

TypeError:只能将str(而不是int)连接到str。
以下是我代码的一小部分:

from tkinter import *
import statistics

root = Tk()

class stats:

    def __init__(self, constant):

        # Determines the type of stat used. e.g-> mean, median, mode, etc...
        self.types_list = ["mean", "median"]
        self.type = self.types_list[1]

        self.values = []

        self.answer_label = Label(constant, text = "", bg = "light green")
        self.answer_label.place(relx = .2, rely = .7, relwidth = .3, relheight = .2)

        self.type_label = Label(constant, text = "It is set to Median", bg = "light green")
        self.type_label.place(relx = .843, rely = 0, relwidth = .15, relheight = .1)

        self.input_data = Entry(constant)
        self.input_data.place(relx = .45, rely = .05, relwidth = .25, relheight = .05)

        self.check = Button(constant, text = "Check", bg = "red", command = self.get_values)
        self.check.place(relx = .65, rely = .05, relwidth = .1, relheight = .05)

        self.mean = Button(constant, text = "Mean", command = self.set_mean)
        self.mean.place(relx = .4, rely = .5, relwidth = .2, relheight = .2)

        self.median = Button(constant, text = "Median", command = self.set_median)
        self.median.place(relx = .2, rely = .5, relwidth = .2, relheight = .2)

    def set_mean(self):

        self.type = self.types_list[0]
        self.type_label.config(text = "It is set to Mean")

    def set_median(self):

        self.type = self.types_list[1]
        self.type_label.config(text = "It is set to Median")

    def get_values(self):

        # Iterates through each number in entry widget

        self.values = self.input_data.get()

        # If it's mean
        if self.type == self.types_list[0]:

            mean = statistics.mean(eval(self.values))
            self.answer_label.config(text = mean)

        # If it's median
        elif self.type == self.type_label[1]:

            median = statistics.median(eval(self.values))
            self.answer_label.config(text = median)

s = stats(root)

mainloop()
但是,当我
打印(statistics.median(eval(self.values))
时,如果不将
类型设置为
median
,它就会工作


如果有人愿意帮忙,我将不胜荣幸!或者如果有任何改进——更好的变量名、简单性(我会修正),或者即使我的问题太长——那么在答案中添加注释或内容。如果您需要进一步澄清我的问题,也请发表评论。

我认为错误在
elif self.type==self.type\u label[1]
中。它应该是
elif self.type==self.types\u list[1]
。re:编码改进-阅读并开始以下内容。它建议的一件事是不要在函数调用中传递的值的`=`周围放置空格字符,这样做会使代码更短,而且通常更可读。