在tkinter中找到中位数时为什么会有错误?

问题描述

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

我评估了(使用 import image img = image.Image("cy.jpg") win = image.ImageWin(img.getWidth(),img.getHeight()) img.draw(win) img.setDelay(1,15) # setDelay(0) turns off animation for row in range(img.getHeight()): for col in range(img.getWidth()): p = img.getPixel(col,row) newred = 255 - p.getRed() newgreen = 255 - p.getGreen() newblue = 255 - p.getBlue() newpixel = image.Pixel(newred,newgreen,newblue) img.setPixel(col,row,newpixel) img.draw(win) win.exitonclick() 的值以找到中位数,但是它给出了以下错误

eval()

这是我的代码的一小部分:

TypeError: can only concatenate str (not int) to 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,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,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() 且未将print(statistics.median(eval(self.values)))设置为type时,它起作用。

如果有人愿意提供帮助,我将很荣幸!或者,如果有任何改进-更好的变量名,简单性(我将解决),或者即使我的问题过长,也可以注释或包括在答案中。如果您需要进一步澄清我的问题,也请发表评论

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)