-1

I'm stuck trying to figure this out. I have looked at other stackoverflow threads on this, and still can't seem to resolve it. Why does my code not work the first time I run it, when I open PyCharm, but it does work the second time?

The first time I always get the error "TypeError: calcFolders() missing 1 required positional argument: 'self'"

This program is a simple Tkinter app that allows me to generate a range folders named with any prefix, with an incremental suffix. FOLDER_1, FOLDER_2, FOLDER_3 etc.

from tkinter import *
from tkinter import ttk
import folderNames
import createDirs

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()
adirectory = StringVar()
prefix = StringVar()
startN = IntVar()
endN = IntVar()
useDir = StringVar()
def calcFolders(self):
    try:
        if len(adir_entry.get())==0:
            useDir = dir_entry.get()
        else:
            useDir = "C:\\Data\\92175\\My Documents\\ARTS\\ACTION\\"+adir_entry.get()+"\\"
        a = folderNames.getf(start_entry.get(), end_entry.get(), pref_entry.get())
        createDirs.createD(useDir,a)
    except ValueError:
        pass

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

adir_entry = ttk.Entry(mainframe, width=14, textvariable = adirectory)
adir_entry.grid(column=2, row=2, sticky=(W, E))

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

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

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

ttk.Label(mainframe, text="Directory").grid(column=1, row=1, sticky=E)
ttk.Label(mainframe, text="or: ARTS # such as \"A0380\"").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="Prefix").grid(column=1, row=3, sticky=E)
ttk.Label(mainframe, text="Start").grid(column=1, row=4, sticky=E)
ttk.Label(mainframe, text="End").grid(column=3, row=4, 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
  • 2
    None of your code is an actual class so having a function that has `self` in it doesn't make since here. I suspect you copied that method out of a class and are trying to use it in a non OOP way. So to do this remove `self` and you should be fine. As it stands now `self` is being treated as a required positional argument so you must pass something to the function. – Mike - SMT Jan 29 '20 at 19:31

1 Answers1

2

calcFolders isn't an instance method of some class, you don't need the usual self argument that refers to the instance of that class. For more about self, refer to this question. Just change your definition to:

def calcFolders():
    try:
        if len(adir_entry.get())==0:
            useDir = dir_entry.get()
        else:
            useDir = "C:\\Data\\92175\\My Documents\\ARTS\\ACTION\\"+adir_entry.get()+"\\"
        a = folderNames.getf(start_entry.get(), end_entry.get(), pref_entry.get())
        createDirs.createD(useDir,a)
    except ValueError:
        pass

You should take a look at what arguments get sent to the command callback when your button is clicked. It looks like tkinter doesn't expect the callback to accept any arguments though.

Bahrom
  • 4,752
  • 32
  • 41