0

I'm trying to create a simple Tkinter app that will create folders within a specified directory. For instance, if you give it the prefix "folder_" and start = 1, end = 5, it will create folder_1, folder_2, folder_3, folder_4, folder_5. I can figure out this logic later, but right now I am having trouble understanding why whatever I type in the "start" Entry box is duplicated in the "end" Entry box. I created a test Entry box beside the Directory, and saw the same thing. What am I missing?

Any help would be much appreciated

Edit: The issue was with declaring variables startN and endN = int(). I changed them to = IntVar() and now when I type in the GUI entry box, I can put different values for start & end.

[![Screensnip of GUI][1]][1]from tkinter import *
from tkinter import ttk
import folderNames as folders


root = Tk()
root.title("Create Folders")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
root.columnconfigure(0, weight=1)
root.rowconfigure(0, weight=1)

directory = StringVar()
test = StringVar()
prefix = StringVar()
startN = int()
endN = int()

def calcFolders(*args):
    try:
        a = folders.getf(start_entry.get(), end_entry.get(), pref_entry.get())
        return(a)
    except ValueError:
        pass

dir_entry = ttk.Entry(mainframe, width=7, textvariable = directory)
dir_entry.grid(column=2, row=1, sticky=(W, E))

test_entry = ttk.Entry(mainframe, width=7, textvariable = test)
test_entry.grid(column=4, row=1, sticky=(W, E))

pref_entry = ttk.Entry(mainframe, width=7, textvariable = prefix)
pref_entry.grid(column=2, row=2, sticky=(W, E))

start_entry = ttk.Entry(mainframe, width=7, textvariable = startN)
start_entry.grid(column=2, row=3, sticky=(W, E))

end_entry = ttk.Entry(mainframe, width=7, textvariable = endN)
end_entry.grid(column=4, row=3, sticky=(W, E))

ttk.Label(mainframe, text="Directory").grid(column=1, row=1, sticky=E)
ttk.Label(mainframe, text="Prefix").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="Start").grid(column=1, row=3, sticky=E)
ttk.Label(mainframe, text="End").grid(column=3, row=3, sticky=E)
ttk.Button(mainframe, text="CREATE", command=calcFolders()).grid(column=1, row=4, sticky=N)



for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

dir_entry.focus()
root.bind('<Return>', calcFolders())

root.mainloop()
Wes Tomer
  • 319
  • 1
  • 13
  • 3
    `int()` produces the number 0. It does not produce a Tkinter Var of any sort, which are the only things you can meaningfully pass as the `textvariable=` option of an Entry. You want `IntVar()` there. For your 'test' and 'directory' Entries, the problem is that you gave both of them the same Var, so they necessarily show the same value. – jasonharper Jan 21 '20 at 15:31
  • 1
    The `Button` initializer expects a function (as well as `root.bind`). So I think that you should use `ttk.Button(mainframe, text="CREATE", command=calcFolders)` and `root.bind('', calcFolders)`. – rassar Jan 21 '20 at 15:32
  • This is a common problem where you are using `()` when you should not be. – Mike - SMT Jan 21 '20 at 15:49
  • 1
    Does this answer your question? [Why is Button parameter “command” executed when declared?](https://stackoverflow.com/questions/5767228/why-is-button-parameter-command-executed-when-declared) – Mike - SMT Jan 21 '20 at 15:49
  • using IntVar() solved the problem. Thanks! – Wes Tomer Jan 21 '20 at 16:09

0 Answers0