【问题标题】:Calculations on user input in Entry box Python tkinter输入框Python tkinter中用户输入的计算
【发布时间】:2018-09-17 16:52:10
【问题描述】:

我只是有一个关于用户通过输入框输入的计算的快速问题。我很困惑为什么没有在标记为“CCalc”的“def浓度”函数中执行计算。我想错误来自无法从前一个函数获取用户输入“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 回调异常 回溯(最近一次通话最后): 调用中的文件“C:\Users\Kevin\Anaconda3\lib\tkinter__init__.py”,第 1699 行 返回 self.func(*args) 文件“C:/Users/Kevin/Desktop/Python/IHCprotocol.py”,第 13 行,集中 tbschk=float(tbsCalc.get()) AttributeError: 'Label' 对象没有属性 '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()

【问题讨论】:

  • “未执行计算”是什么意思?现代的说法是“它抛出异常”吗?如果是这样,您应该在问题中包含错误消息和完整的回溯。
  • 看来是该睡午觉了……

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


【解决方案1】:

你不能 get 显示为 label 的值,这就是错误告诉你的,解决我创建了名为 e1entry widget 并且没有在窗口中定位,所以我可以获取其中的值。您的窗口看起来相同,但现在可以显示结果。

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

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2015-09-09
    相关资源
    最近更新 更多