【问题标题】:TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType' errorTypeError:int() 参数必须是字符串、类似字节的对象或数字,而不是“NoneType”错误
【发布时间】:2019-09-16 11:22:42
【问题描述】:

我是 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”中使用的方程式。

【问题讨论】:

  • callback2() 返回无... callback 相同... 也许您应该直接使用年龄和值变量

标签: python python-3.x tkinter


【解决方案1】:

您的回调没有返回语句,因此它们实际上返回None。所以在这些行中:

m = callback()
p = int(m)
a = callback2()
n = int(a)

ma 都被分配了None,所以你打电话给int(None)。您可能的意思是:

def callback():
    value=(msrp.get())
    return value

def callback2():
    age=(old.get())
    return age

【讨论】:

    【解决方案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()
    

    请注意,valueage 仅在本地范围内限定为它们自己的函数,因此将它们放在按钮回调中没有任何作用

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-06
      • 1970-01-01
      • 2017-11-14
      • 2021-12-04
      相关资源
      最近更新 更多