类型错误:不支持的操作数类型 *: 'Entry' 和 '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)

这就是代码(不要介意我把整个代码翻译成英语。(

我试着在谷歌上搜索答案,但什么也没找到。据我所知,程序应该在窗口中的按钮下方输出结果。

我经常遇到的错误是:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:UsersspajiAppDataLocalProgramsPythonPython310libtkinter__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'

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

m = float(input1.get())
m=input1
g=9.81
h=input2
P=input3
time=m*g*h/P

你能看到这些线条吗?gfloat,而hmPtkinter.Entry对象,不能将它们相乘。


你可以用这种方式相乘:

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

并且用CCD_ 9和CCD_。

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

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

相关内容

最新更新