计算输入框中的用户输入 Python tkinter

Calculations on user input in Entry box Python tkinter

我有一个关于用户通过输入框输入的计算的快速问题。我很困惑为什么没有在标记为 "CCalc" 的 "def concentration" 函数中执行计算。我想错误是由于代码无法从上一个函数获取用户输入 "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()

我收到错误消息:

Exception in Tkinter callback Traceback (most recent call last): File "C:\Users\Kevin\Anaconda3\lib\tkinter__init__.py", line 1699, in call return self.func(*args) File "C:/Users/Kevin/Desktop/Python/IHCprotocol.py", line 13, in concentration tbschk=float(tbsCalc.get()) AttributeError: 'Label' object has no attribute '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()

您不能 get 您显示为 label 的值,这就是错误告诉您的内容,为了解决我创建的 entry widget 命名为 e1 和没有在 window 中定位,因此我可以获得 it.You 中的值 window 看起来相同,但现在可以显示结果。

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()