计算tkinter中的输入输入

时间:2018-02-18 21:43:21

标签: python tkinter

我正在使用Idle和tkinter.messagebox。我试图从输入输入显示计算的结果。我一直无法弄清楚如何捕获用户条目,然后运行计算并生成结果。

所以,我希望用户能够输入数字,例如2和2.然后,我希望用户按下按钮。接下来,2 + 2的答案应出现在空白条目标签中。

我的问题是如何捕获用户输入并在计算中使用它们,然后显示结果?

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 *
master = Tk()
label1 = Label(master, text = 'Bid Number', relief = 'groove', width = 16)
label2 = Label(master, text = 'Cost Score', relief = 'groove', width = 16)
label3 = Label(master, text = 'Past Performance', relief = 'groove', width = 16)
label8 = Label(master, text = 'Bid 1' , relief = 'groove', width = 12)
entry1 = Entry(master, relief = 'groove', width = 12)
entry2 = Entry(master, relief = 'groove', width = 12)
label9 = Label(master, text = 'Bid Score', relief = 'groove', width = 16)

def button_function():
    a = float( mydict[entry1.get()] ) * float( mydict[entry2.get()] )

button1 = Button(master, text = 'calculate', relief = 'groove', width = 12, command = button_function)
label16 = Frame(master, print(a))


#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 )
label8.grid( row = 2, column = 1, padx = 10 )
entry1.grid( row = 2, column = 2, padx = 10 )
entry2.grid( row = 2, column = 3, padx = 10 )
label9.grid( row = 1, column = 8, padx = 10 )
label16.grid( row = 2, column = 8, padx = 10 )
button1.grid( row = 3, column = 4, columnspan = 2) 

2 个答案:

答案 0 :(得分:0)

我不认为这个问题可以帮助任何人,但是你处于当前状态,但我认为你想要实现的目标可以通过替换:

def button_function():
    a = float( mydict[get(entry1)] ) * float( mydict[get(entry1)] ) * float( mydict[get(entry1)] ) *float( mydict[get(entry2)] ) * float( mydict[get(entry3)] ) *float( mydict[get(entry4)] ) * float( mydict[get(entry5)] ) * float( mydict[get(entry5)] ) * float( mydict[get(entry6)]) 
    ...

使用:

def button_function():
    a = float( mydict[entry1.get()] ) * float( mydict[entry1.get()] )\
    * float( mydict[entry1.get()] ) *float( mydict[entry2.get()] )\
    * float( mydict[entry3.get()] ) *float( mydict[entry4.get()] )\
    * float( mydict[entry5.get()] ) * float( mydict[entry5.get()] )\
    * float( mydict[entry6.get()])
    ...

此外,添加:

label16.grid()
button1.grid()

为了显示,未显示但显然需要显示小部件。

答案 1 :(得分:0)

我解决了这个问题。我需要定义show_answer并将其插入空白,然后命令按钮显示答案。谢谢你的帮助。

     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 
*from tkinter.messagebox import *


    master = Tk()
    label1 = Label(master, text = 'Bid Number', relief = 'groove', width = 16)
    label2 = Label(master, text = 'Cost Score', relief = 'groove', width = 16)
    label3 = Label(master, text = 'Past Performance', relief = 'groove', width = 16)
    label8 = Label(master, text = 'Bid 1' , relief = 'groove', width = 12)
    entry1 = Entry(master, relief = 'groove', width = 12)
    entry2 = Entry(master, relief = 'groove', width = 12)
    label9 = Label(master, text = 'Bid Score', relief = 'groove', width = 16)
    blank1 = Entry(master, relief = 'groove', width = 16)

    def button_function():
        float( mydict[entry1.get()] ) * float( mydict[entry2.get()] )

    def show_answer():
        Ans = float( mydict[entry1.get()] ) * float( mydict[entry2.get()] )
        blank1.insert(0, Ans)

    button1 = Button(master, text = 'calculate', relief = 'groove', width = 12, command =show_answer)


    #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 )
    label8.grid( row = 2, column = 1, padx = 10 )
    entry1.grid( row = 2, column = 2, padx = 10 )
    entry2.grid( row = 2, column = 3, padx = 10 )
    label9.grid( row = 1, column = 8, padx = 10 )
    blank1.grid( row = 2, column = 8, padx = 10 )
    button1.grid( row = 3, column = 4, columnspan = 2)