我是Tkinter的新手,它说可以转换为字符串,但是我的输入是整数,当我运行它时,会出现以下错误:
TypeError:int()参数必须是字符串,类似字节的对象或 数字,而不是“ NoneType”
import tkinter as tk
window9 = tk.Tk()
msrp = tk.IntVar()
amgpage = tk.Label(window9, text="Mercedes Benz AMG Depreciation Calculator").pack(anchor='center')
amgpage = tk.Label(window9, text="What is the MSPR of the car?: ")
amgpage.pack()
msrp = tk.Entry(window9)
msrp.pack()
msrp.focus_set()
def callback():
value=(msrp.get())
b = tk.Button(window9, text="Save your msrp value", command=callback,fg="red")
b.pack()
amgpage = tk.Label(window9, text="What is the age of the car?: ")
amgpage.pack()
old = tk.Entry(window9)
old.pack()
old.focus_set()
def callback2():
age=(old.get())
b = tk.Button(window9, text="Save the age of the car", command=callback2,fg="blue")
b.pack()
amgpage = tk.Label(window9, text="")
amgpage.pack(anchor='w')
def msrpv():
m = callback()
p = int(m)
a = callback2()
n = int(a)
a=p*(1-0.15)**n
amgpage=tk.Label(window9,text="$"+a)
amgpage.pack()
amgmsrp = tk.Button(window9, text="Get the current value of the car.", command=msrpv,fg="green")
amgmsrp.pack()
window9.geometry("400x400")
window9.title("Mercedes Benz AMG Depreciation Calculator")
window9.mainloop()
我想使用用户给我的数字,并将其插入在程序“ a = p *(1-0.15)** n”中使用的方程式中。
答案 0 :(得分:3)
您的回调没有return语句,因此它们实际上正在返回None
。因此,在这些行中:
m = callback()
p = int(m)
a = callback2()
n = int(a)
m
和a
都被分配了None
,因此您正在呼叫int(None)
。您可能打算做类似的事情:
def callback():
value=(msrp.get())
return value
和
def callback2():
age=(old.get())
return age
答案 1 :(得分:2)
您根本不需要“回调”。
直接获取值
def msrpv():
p = int(msrp.get())
n = int(old.get())
a=p*(1-0.15)**n
amgpage=tk.Label(window9,text="$"+a)
amgpage.pack()
请注意,value
和age
仅在本地范围内作用于它们自己的功能,因此将它们置于按钮回调中并没有做任何事情