我无法在python 3.6中获取条目数据

时间:2017-05-02 22:17:49

标签: python tkinter python-3.6

我为python学习制作了小型医疗计算器程序。

在这个程序中,当我按下" Calc"按钮,它应该在我的命令行中显示。但它不起作用。

此外,我怀疑定义的功能是在没有按钮点击的情况下操作的。

我有什么错误?

#importing modules
from tkinter import * 

#setting up window
top = Tk()
F = Frame(top)
F.master.title("FeNa Calculator")
F.pack()

#Calc. button event handler
def fena_click():
    ur_na = tUNa.get()
    ur_cr = tUCr.get()
    se_na = tSNa.get()
    se_cr = tSCr.get()

    print('Urine Na : ' + ur_na, end=' ')
    print('Urine Cr : ' + ur_cr, end=' ')
    print('Serum Na : ' + se_na, end=' ')
    print('Serum Cr : ' + se_cr, end=' ')

#add widgets
unit1 = Label(F, text="mEq/L")
unit2 = Label(F, text="mEq/L")
unit3 = Label(F, text="mEq/L")
unit4 = Label(F, text="mEq/L")
UNa = Label(F, text="Urine Na")
tUNa = Entry(F)
UCr = Label(F, text="Urine Cr")
tUCr = Entry(F)
SNa = Label(F, text="Serum Na")
tSNa = Entry(F)
SCr = Label(F, text="Serum Cr")
tSCr = Entry(F)
blank1 = Label(F, text="")
v_Result = StringVar()
Result = Label(F, textvariable = v_Result)
v_Result.set("FENa(%) = ")
blank2 = Label(F, text="")
bCalc = Button(F, text="Calc.", command = fena_click())
bQuit = Button(F, text="Quit", command = F.quit)


UNa.grid(row = 0, column = 0, pady = 2)
tUNa.grid(row =0, column = 1, pady = 2)
unit1.grid(row = 0, column = 2, pady = 2)
UCr.grid(row = 1, column = 0, pady = 2)
tUCr.grid(row =1, column = 1, pady = 2)
unit2.grid(row = 1, column = 2, pady = 2)
SNa.grid(row = 2, column = 0, pady = 2)
tSNa.grid(row =2, column = 1, pady = 2)
unit3.grid(row = 2, column = 2, pady = 2)
SCr.grid(row = 3, column = 0, pady = 2)
tSCr.grid(row =3, column = 1, pady = 2)
unit4.grid(row = 3, column = 2, pady = 2)
blank1.grid(row=4, column = 0, columnspan = 3)
Result.grid(row = 5, column = 0, columnspan = 3)
blank2.grid(row=6, column = 0, columnspan = 3)
bCalc.grid(row = 7, column = 0, columnspan = 2)
bQuit.grid(row = 7, column = 1, columnspan = 2)

#loop running
F.mainloop()

Program view

1 个答案:

答案 0 :(得分:0)

在您创建按钮的行中,您实际上正在调用fena_click函数。你需要传递函数本身,你可以通过省略paranthesis来完成:

bCalc = Button(F, text="Calc.", command = fena_click) # no () after fena_click