在条目框Python tkinter中计算用户输入

时间:2018-04-08 01:13:00

标签: python python-3.x tkinter spyder tkinter-entry

我只是通过Entry Box从用户输入中得到一个关于计算的快速问题。我很困惑为什么计算没有在" def浓度"中进行。功能标记为" CCalc"。我想错误来自代码无法从上一个函数获得用户输入" tbschk"。我还需要考虑一些其他方法来确定如何正确地做到这一点吗?

import tkinter as tk
from tkinter import Label, Button, Entry

#multiply total brain by 1000ul
def tbscalc():
    Bchk=float(BNum.get())
    BCalc = (Bchk*1000)
    tbsCalc["text"]=str(BCalc)

#divide concentration by total TBSul
def concentration():
    Cchk=float(Conc.get())
    tbschk=float(tbsCalc.get())
    CCalc=(tbschk/Cchk)
    PCalc["text"]=str(CCalc)

window=tk.Tk()
window.geometry("500x500")

#Using total brain to calculate TBS
BLabel=Label(window, text="Enter number of brains")
tbsLabel=Label(window, text="Amount of TBS needed in ul")
tbsCalc=Label(window)
BNum=Entry(window)
BTBSbtn=Button(window, text="Calculate", command=tbscalc)

#Using concentration to calculate total primary
CLabel=Label(window, text="Enter primary concentration")
PLabel=Label(window, text="This is how much primary antibody you need")
PCalc=Label(window)
Conc=Entry(window)
TPbtn=Button(window, text="Calculate", command=concentration)

#Locations
BLabel.grid(row=0 ,column=0)
BNum.grid(row=0 ,column=1)
tbsLabel.grid(row=1 ,column=0)
tbsCalc.grid(row=1 ,column=1)
BTBSbtn.grid(row=2 ,column=0)
CLabel.grid(row=3, column=0)
Conc.grid(row=3, column=1)
PLabel.grid(row=4, column=0)
PCalc.grid(row=4, column=1)
TPbtn.grid(row=5, column=0)

window.mainloop()

我收到错误消息:

  

Tkinter回调中的异常       Traceback(最近一次调用最后一次):         文件" C:\ Users \ Kevin \ Anaconda3 \ lib \ tkinter__init __。py",第1699行,致电           return self.func(* args)         File" C:/Users/Kevin/Desktop/Python/IHCprotocol.py" ;,第13行,专注于           tbschk =浮子(tbsCalc.get())       AttributeError:'标签'对象没有属性' get'

它引用的错误

class CallWrapper:
    """Internal class. Stores function to call when some user
    defined Tcl function is called e.g. after an event occurred."""
    def __init__(self, func, subst, widget):
        """Store FUNC, SUBST and WIDGET as members."""
        self.func = func
        self.subst = subst
        self.widget = widget
    def __call__(self, *args):
        """Apply first function SUBST to arguments, than FUNC."""
        try:
            if self.subst:
                args = self.subst(*args)
            return self.func(*args)
        except SystemExit:
            raise
        except:
            self.widget._report_exception()

1 个答案:

答案 0 :(得分:1)

您不能get显示为label的值,而该错误告诉您,要解决我创建的entry widget名为e1并且没有&# 39; t在窗口中的位置,以便我可以得到它的值。你的窗口看起来相同,但现在能够得到结果显示。

import tkinter as tk
from tkinter import Label, Button, Entry

#multiply total brain by 1000ul

def tbscalc():
    Bchk=float(BNum.get())
    BCalc = (Bchk*1000)
    tbsCalc["text"]=str(BCalc)
    e1.insert(0, BCalc) # this receiving the answer in the so that i can be return

#divide concentration by total TBSul
def concentration():
    Cchk=float(Conc.get())
   # tbschk=float(tbsCalc.get())
    tbschk = float(e1.get()) # this getting the value in the entry widget
    CCalc=(tbschk/Cchk)
    PCalc["text"]=str(CCalc)


window=tk.Tk()
window.geometry("500x500")

#Using total brain to calculate TBS
BLabel=Label(window, text="Enter number of brains")
tbsLabel=Label(window, text="Amount of TBS needed in ul")
tbsCalc=Label(window)
BNum=Entry(window)
BTBSbtn=Button(window, text="Calculate", command=tbscalc)

#Using concentration to calculate total primary
CLabel=Label(window, text="Enter primary concentration")
PLabel=Label(window, text="This is how much primary antibody you need")
PCalc=Label(window)
Conc=Entry(window)
TPbtn=Button(window, text="Calculate", command=concentration)

e1 = Entry(window) # this new entry i created but didn't position
#e1.grid(row=5, column=30)

#Locations
BLabel.grid(row=0 ,column=0)
BNum.grid(row=0 ,column=1)
tbsLabel.grid(row=1 ,column=0)
tbsCalc.grid(row=1 ,column=1)
BTBSbtn.grid(row=2 ,column=0)
CLabel.grid(row=3, column=0)
Conc.grid(row=3, column=1)
PLabel.grid(row=4, column=0)
PCalc.grid(row=4, column=1)
TPbtn.grid(row=5, column=0)

window.mainloop()