如何修复“TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'”错误

5

我正在尝试创建一个python文件,用于向目录发送.txt文件的垃圾邮件。

我决定开始使用Tkinter,但每当我尝试输入数字时,出现以下错误消息"TypeError:int()参数必须是字符串、类似字节的对象或数字,而不是'NoneType'"

我正在使用的代码是:

from tkinter import *  

top = Tk()  

top.geometry("400x250")  

Amount = Label(top, text = "Amount").place(x = 30,y = 50)  

def spam():
    for i in range(int(e1)):
        print(i)

sbmitbtn = Button(top, text = "Submit",activebackground = "pink", activeforeground = "blue",command=spam).place(x = 30, y = 170)  

e1 = Entry(top).place(x = 80, y = 50)  



top.mainloop()  

我尝试将 for i in range(int(e1)): 替换为 for i in range(str(e1)): 但是我得到了错误消息:

"TypeError: 'str' object cannot be interpreted as an integer"

有所帮助总是好的

2个回答

2
使用get()方法来获取Entry的值。例如:
def spam():
    for i in range(int(e1.get())):
        print(i)

不要将条目放在同一行中:

错误的方式:

最初的回答:

e1 = Entry(top).place(x = 80, y = 50)

"最初的回答"

正确:

e1 = Entry(top)
e1.place(x = 80, y = 50)  

0
你应该先获取输入框内的值,然后将其转换为整数。
def spam():
  entry_val = e1.get()
  for i in range(int(entry_val)):
    print(i)

好的,我已经按照你给我的代码进行了更新,但现在我遇到了错误:"AttributeError: 'NoneType' object has no attribute 'get'"。 - Lazer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接