TypeError: unsupported operand type(s) for *: 'Entry' and 'float'

TypeError: unsupported operand type(s) for *: 'Entry' and 'float'

好的,所以我有一个正在学习的家庭成员Python。除了Hello World,我自己也不知道Python。她用 tkinter 做了一个代码,它一直返回标题中提到的错误。这是代码:

from tkinter import*
def time():
    m=input1
    g=9.81
    h=input2
    P=input3
    time=m*g*h/P
    time=Label(window1, text="Time is"+str(time))
    time.place(x=130, y=230)
window1=Tk()
window1.title("TASK 1")
window1.geometry("300x300")

output1=Label(window1, text="WORK-POWER-ENERGY")
output1.place(x=70, y=20)

output2=Label(window1, text="Mass of the object (t):")
output2.place(x=40,y=60)

input1=Entry(window1)
input1.place(x=160,y=60, width=80)

output3=Label(window1,text="Height of lifting (m):")
output3.place(x=20,y=100)

input2=Entry(window1)
input2.place(x=160, y=100, width=80)

output4=Label(window1,text="Power of the elevator (kW):")
output4.place(x=10,y=140)

input3=Entry(window1)
input3.place(x=160, y=140, width=80)

button1=Button(window1,text="Calculate", command=time)
button1.place(x=150,y=180)

这就是代码(请不要介意我尝试将整个代码翻译成英文。)

我已经尝试通过谷歌搜索寻找答案,但没有任何结果。据我所知,该程序应该在 window.

中的按钮下方输出结果

我经常遇到的错误是这样的:

Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Users\spaji\AppData\Local\Programs\Python\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "D:/Korisnici/spaji/Radna površina/programmm.py", line 7, in time
    time=m*g*h/P
TypeError: unsupported operand type(s) for *: 'Entry' and 'float'
m=input1
g=9.81
h=input2
P=input3
time=m*g*h/P

你能看到这几行吗? g 是一个 float,而 hmPtkinter.Entry 个对象,不能将它们相乘。


你可以这样相乘:

m = input1.get() # You get the content of input1 as a string
m = float(m) # You convert it to a float

...并使用 hP 执行此操作。

input1input2input3 变量属于 Entry 类型 - 它们代表整个输入栏,而不是输入到其中的任何值。您不能将 UI 元素乘以数字,因为它没有意义。如果你想要一个值本身,你需要一个 .get() 方法(returns 一个字符串 - 如果你想要一个数字,你必须进行另一次转换)。

https://www.tutorialspoint.com/python/tk_entry.htm

您必须使用 get 方法提取存储在 Entry 对象中的值(然后适当地解析该字符串)。例如,

m = float(input1.get())