john doe john doe - 6 months ago 38
Python Question

AttributeError: StringVar instance has no attribute 'endswith' while trying to call from a Tkinter button

I would like to create a GUI that receives two paths (a directory full of .txt documents and the destination of a new .csv file created from the files of the previously mentioned folder).

enter image description here

Therefore I tried the following:

import Tkinter as tk
import glob, os, csv, codecs
# Create instance
win = tk.Tk()

# Add a title
win.title("GUI")

# Disable resizing the GUI
#win.resizable(0,0)
#
## Modify adding a Label
aLabel = tk.Label(win, text="Transform a directory \n"
"full of opinions to a \n"
".csv file")
aLabel.grid(column=1, row=1)

#==================
# Functionality (all the functions go here)
#==================
def retrive(directory_path):
for filename in sorted(glob.glob(os.path.join(directory_path, '*.txt'))):
with open(filename, 'r') as f:
important_stuff = f.read().splitlines()
oneline = [''.join(important_stuff)]
yield filename.split('/')[-1] + ', ' +str(oneline).strip('[]"')

def munge(directory,directory2):
test = tuple(retrive(directory))
with codecs.open(directory2,'w', encoding='utf8') as out:
csv_out=csv.writer(out, delimiter='|')
csv_out.writerow(['id','content'])
for row in test:
csv_out.writerow(row.split(', ', 1))
#==================
# The UI goes here:
#==================


# Changing our Label
tk.Label(win, text="Path of the input file:").grid(column=2, row=0)

# Adding a Textbox Entry widget (path of the input file)
input_directory = tk.StringVar()
nameEntered = tk.Entry(win, width=12, textvariable=input_directory)
#input_directory.set(input_directory)
#Position of the widget
nameEntered.grid(column=2, row=1)

# Changing our Label
tk.Label(win, text="Path of the output file:").grid(column=11, row=0)

# Adding a Textbox Entry widget (path of the output file)
output_directory = tk.StringVar()
#output_directory.set(output_directory)
nameEntered = tk.Entry(win, width=12, textvariable=output_directory)
nameEntered.grid(column=11, row=1)



# Adding a Button
action = tk.Button(win, text="To .csv", command=munge(input_directory,output_directory))
action.grid(column=20, row=1)

#======================
# Start GUI
#======================
win.mainloop()


I am having trouble calling the function
munge()
:

action = tk.Button(win, text="To .csv",command=munge(input_directory,output_directory))


Nevertheless, this exception raised:

/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/bin/python2.7 /Users/user/PycharmProjects/script.py
Traceback (most recent call last):
File "/Users/user/PycharmProjects/script.py", line 82, in <module>
action = tk.Button(win, text="To .csv", command=munge(input_directory,output_directory))
File "/Users/user/PycharmProjects/script.py", line 39, in munge
test = tuple(retrive(directory))
File "/Users/user/PycharmProjects/script.py", line 31, in retrive
for filename in sorted(glob.glob(os.path.join(directory_path, '*.txt'))):
File "/usr/local/Cellar/python/2.7.10_2/Frameworks/Python.framework/Versions/2.7/lib/python2.7/posixpath.py", line 70, in join
elif path == '' or path.endswith('/'):
AttributeError: StringVar instance has no attribute 'endswith'

Process finished with exit code 1


How can I correctly call the aforementioned function with the help of the Button widget?. I tried to set the name of the variable as indicated here question, but it did not worked.

update

Then from an answer of this question, I tried the following:

action = tk.Button(win, text="To .csv", command=lambda:munge(input_directory,output_directory))

Answer

After all, it worked with: action = tk.Button(win, text="To .csv", command=lambda:munge(input_directory.get(),output_directory.get())). However, from Bryan Oakley answer I belive that this is not the correct way to do this.

Comments