roy
roy

Reputation: 53

win 10 python 3.5 tkinter entry ValueError: invalid literal for int() with base 10: ''

i have written a program that creates numberd directories inside other directories it works fine except one little issue that is the most important. the entry that is used in the while loop can't convert into a base 10 integer. i have tried several ways and found nothing that would help me..

from tkinter import *
from tkinter import filedialog as fd
import tkinter.messagebox as box
from tkinter import ttk
import os

#window propeties
window = Tk()
frame = Frame(window)
entry = Entry(frame)
window.wm_iconbitmap('icon.ico')
img = PhotoImage('')
directory = "C:\ "

#window geometry
window.title("numbered Directories creater")
window.geometry("600x400")

#label & buttons
explain_lab_1 = Label(window, text = "This is a tool used to create directories with matching numbers")
explain_lab_2 = Label(window, text = "have fun!!!")
explain_lab_3 = Label(window, text = "\bstay safe\b")
path_btn = ttk.Button(window, text = "Change path")
lab_path = Label(window, text = directory)
create_btn = ttk.Button(window, text = "CREATE")
num_entry = ttk.Entry(window)
lab_to_text_nums = Label(window, text = "fill the number of directories you 
wish to create")
print(type(num_entry.get()))
num_entry_in = num_entry.get()

#functions
def say_dir():
    box.showinfo("Directory creater", "The directory to create is set to C:\ 
that ia a default")

def change_path():
    directory = fd.askdirectory()
    lab_path.configure(text = directory)

def flood(i=0):
    while i < num_entry_in:
        try:
            os.mkdir(directory + str(i))
            i += 1
        except FileExistsError:
            i += 1

#configurations
path_btn.configure(command = change_path)
create_btn.configure(command = flood)
explain_lab_1.configure(font=("Courier", 9))
explain_lab_2.configure(font=("Courier", 9))
explain_lab_3.configure(font=("Courier", 9))
lab_to_text_nums.configure(font=("Courier", 9))

#show labs & buttons
explain_lab_1.grid(row = 1, column = 0)
explain_lab_2.grid(row = 2, column = 0)
explain_lab_3.grid(row = 3, column = 0)
path_btn.grid(row = 6, column = 2)
lab_path.grid(row = 7, column = 1 , columnspan = 2)
create_btn.grid(row = 9, column = 2)
lab_to_text_nums.grid(row = 8, column = 0)
num_entry.grid(row = 9, column = 0)

#call functions
say_dir()
#loads window
window.mainloop()

Upvotes: 0

Views: 88

Answers (1)

Hugh Bothwell
Hugh Bothwell

Reputation: 56694

The problem is when you call

num_entry_in = num_entry.get()

Currently you call it right away, as soon as num_entry is created, so of course the field is empty and returns "" (an empty string), and int("") gives ValueError.

You need to put it inside flood() so that it runs when the button is pressed, after the user has entered something. You also need to check the result to make sure it actually is a number, ie

try:
    num_entry_in = int(num_entry.get())
except ValueError:
    # not a an int!
    # pop up a warning to let the user know what went wrong

Upvotes: 1

Related Questions