好的,所以我有一個正在學習 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:\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'
uj5u.com熱心網友回復:
您必須使用該get方法來提取存盤在Entry物件中的值(然后適當地決議該字串)。例如,
m = float(input1.get())
uj5u.com熱心網友回復:
m=input1
g=9.81
h=input2
P=input3
time=m*g*h/P
你能看到這些線條嗎?g是 a ,floatwhile和是物件,你不能將它們相乘。hmPtkinter.Entry
你可以這樣相乘:
m = input1.get() # You get the content of input1 as a string
m = float(m) # You convert it to a float
...并使用 and 執行此h操作P。
uj5u.com熱心網友回復:
input1,input2并且input3變數是 Entry 型別 - 它們代表整個輸入欄,而不是輸入到其中的任何內容的值。您不能將 UI 元素乘以數字,因為它沒有意義。如果你想要一個值本身,你需要一個.get()方法(回傳一個字串 - 如果你想要一個數字,你必須進行另一次轉換)。
https://www.tutorialspoint.com/python/tk_entry.htm
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/452617.html
