python

时间:2018-02-18 16:44:16

标签: python tkinter

我正在尝试计算python中的加权几何平均值(一组数字(f)的乘积被提升到' 1 / f')。我正在使用tkinter messagebox来构建一个gui。我创建了一个关键字:值对字典。键是任意的。这些值是几何平均值计算的输入。我已经定义了' a'作为九个变量(f)的乘积。我已经指定了' b'价值为' 1/9'作为1 / f,变量的数量。但是,我试图使功能异构。这样它就可以改变b的值,这取决于变量的数量(f)。例如,如果' a'有5个变量,那么b应该=' 1/5'。异构几何平均函数在python中会是什么样的?

mydict = {'good':0.7, 'average':0.5, 'optimal': 1, 'unacceptable': 0, 'major innovation': 1, 'minor innovation': 0.7, 'no innovation': 0.4, '1st lowest': 1, '2nd lowest': 0.7, '3rd lowest': 0.5, '4th lowest': 0.3}
a = float( mydict[label9] ) * float( mydict[label9] ) * float( mydict[label9] ) *float( mydict[label10] ) * float( mydict[label11] ) *float( mydict[label12] ) * float( mydict[label13] ) * float( mydict[label13] ) * float( mydict[label14]) 
b = 1/9
c = a**b


    # Widgets:
from tkinter import *
window = Tk()
label1 = Label(window, text = 'Bid Number', relief = 'groove', width = 16)
label2 = Label(window, text = 'Cost Score', relief = 'groove', width = 16)
label3 = Label(window, text = 'Past Performance', relief = 'groove', width = 16)
label4 = Label(window, text = 'Quality Score', relief = 'groove', width = 16)
label5 = Label(window, text = 'Management Score', relief = 'groove', width = 16)
label6 = Label(window, text = 'Innovation Score', relief = 'groove', width = 16)
label7 = Label(window, text = 'Compliance Score', relief = 'groove', width = 16)
label8 = Label(window, text = 'Bid 1' , relief = 'groove', width = 12)
label9 = Entry(window, relief = 'groove', width = 12)
label10 = Entry(window, relief = 'groove', width = 12)
label11 = Entry(window, relief = 'groove', width = 12)
label12 = Entry(window, relief = 'groove', width = 12)
label13 = Entry(window, relief = 'groove', width = 12)
label14 = Entry(window, relief = 'groove', width = 12)
label15 = Label(window, text = 'Bid Score', relief = 'groove', width = 16)
label16 = Label(window, text = c, relief = 'groove', width = 12)
button1 = Button(window, text = 'calculate', relief = 'groove', width = 12)

#Geometry
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 1, column = 2, padx = 10 )
label3.grid( row = 1, column = 3, padx = 10 )
label4.grid( row = 1, column = 4, padx = 10 )
label5.grid( row = 1, column = 5, padx = 10 )
label6.grid( row = 1, column = 6, padx = 10 )
label7.grid( row = 1, column = 7, padx = 10 )
label8.grid( row = 2, column = 1, padx = 10 )
label9.grid( row = 2, column = 2, padx = 10 )
label10.grid( row = 2, column = 3, padx = 10 )
label11.grid( row = 2, column = 4, padx = 10 )
label12.grid( row = 2, column = 5, padx = 10 )
label13.grid( row = 2, column = 6, padx = 10 )
label14.grid( row = 2, column = 7, padx = 10 )
label15.grid( row = 1, column = 8, padx = 10 )
label16.grid( row = 2, column = 8, padx = 10 )
button1.grid( row = 3, column = 4, columnspan = 2)

1 个答案:

答案 0 :(得分:1)

这里有几个问题。主要问题是您在分配变量之前尝试调用变量。您尝试在第一行使用label9,尽管直到18日才定义它。这可以通过将赋值移动到label9之后来解决。

(旁注,名称为9到14的对象实际上是输入框。命名约定应该反映出来。我建议阅读样式指南PEP8)

在当前状态下,系统将无法获取用户键入的值,因为您正在访问标签对象而不是文本值。要获取文本值,请使用.get()命令。

最后,你需要一个函数来运行它,好像它没有链接到一个函数然后a)它将无法正常工作,因为用户没有输入任何东西和b)按钮将是无用的。

此代码具有上述调整(尽管我保留了变量名称)并且正常工作

mydict = {'good':0.7, 'average':0.5, 'optimal': 1, 'unacceptable': 0, 'major innovation': 1, 'minor innovation': 0.7, 'no innovation': 0.4, '1st lowest': 1, '2nd lowest': 0.7, '3rd lowest': 0.5, '4th lowest': 0.3}


# Widgets:
from tkinter import *
window = Tk()
label1 = Label(window, text = 'Bid Number', relief = 'groove', width = 16)
label2 = Label(window, text = 'Cost Score', relief = 'groove', width = 16)
label3 = Label(window, text = 'Past Performance', relief = 'groove', width = 16)
label4 = Label(window, text = 'Quality Score', relief = 'groove', width = 16)
label5 = Label(window, text = 'Management Score', relief = 'groove', width = 16)
label6 = Label(window, text = 'Innovation Score', relief = 'groove', width = 16)
label7 = Label(window, text = 'Compliance Score', relief = 'groove', width = 16)
label8 = Label(window, text = 'Bid 1' , relief = 'groove', width = 12)
label9 = Entry(window, relief = 'groove', width = 12)
label10 = Entry(window, relief = 'groove', width = 12)
label11 = Entry(window, relief = 'groove', width = 12)
label12 = Entry(window, relief = 'groove', width = 12)
label13 = Entry(window, relief = 'groove', width = 12)
label14 = Entry(window, relief = 'groove', width = 12)
label15 = Label(window, text = 'Bid Score', relief = 'groove', width = 16)
label16 = Label(window, text = "", relief = 'groove', width = 12)

def button_function():
    a = float( mydict[label9] ) * float( mydict[label9] ) * float( 
mydict[label9] ) *float( mydict[label10] ) * float( mydict[label11] ) *float( mydict[label12] ) * float( mydict[label13] ) * float( mydict[label13] ) * float( mydict[label14]) 
    b = 1/9
    c = a**b
    label16.config(text=str(c))


button1 = Button(window, text = 'calculate', relief = 'groove', width = 12, command = button_function)



#Geometry
label1.grid( row = 1, column = 1, padx = 10 )
label2.grid( row = 1, column = 2, padx = 10 )
label3.grid( row = 1, column = 3, padx = 10 )
label4.grid( row = 1, column = 4, padx = 10 )
label5.grid( row = 1, column = 5, padx = 10 )
label6.grid( row = 1, column = 6, padx = 10 )
label7.grid( row = 1, column = 7, padx = 10 )
label8.grid( row = 2, column = 1, padx = 10 )
label9.grid( row = 2, column = 2, padx = 10 )
label10.grid( row = 2, column = 3, padx = 10 )
label11.grid( row = 2, column = 4, padx = 10 )
label12.grid( row = 2, column = 5, padx = 10 )
label13.grid( row = 2, column = 6, padx = 10 )
label14.grid( row = 2, column = 7, padx = 10 )
label15.grid( row = 1, column = 8, padx = 10 )
label16.grid( row = 2, column = 8, padx = 10 )
button1.grid( row = 3, column = 4, columnspan = 2)