Lazer
Lazer

Reputation: 73

how to fix "TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'"

I am trying to create a python file that will spam a directory with .txt files.

I decided to start working with Tkinter but whenever I try to input a number I get this error message "TypeError: int() argument must be a string, a bytes-like object or a number, not 'NoneType'"

The code I am working with is:

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()  

I've tired switching the for i in range(int(e1)): to for i in range(str(e1)): but then I get the error message:

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

Any help is good help

Upvotes: 5

Views: 26877

Answers (2)

Nouman
Nouman

Reputation: 7313

Use get() method to get the value of the Entry. Example:

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

And don't place/pack the entry in the same line:

Wrong:

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

Correct:

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

Upvotes: 2

Guillem
Guillem

Reputation: 2647

You should first get the value inside the entry and then cast it to integer.

def spam():
  entry_val = e1.get()
  for i in range(int(entry_val)):
    print(i)

Upvotes: 0

Related Questions