I am having trouble with my Tkinter code. The objective of the code is to have 4 buttons, 2 browse buttons with which to select fiels that are to be compared to one another. The third is to select a destination for the outputted file. The fourth is to pull my python code with which both files will be computed.
I am having trouble with my browse buttons, where, whenever i select a file in the window they return:
pathlabel.delete(0, END)
NameError: name 'pathlabel' is not defined
or
content = infile.read(filename2)
TypeError: integer argument expected, got str"
The intended outcome is for the selected files to have their paths saved, so that I can then pull those paths to be used in my python code.
I've looked for documentation online concerning the pathlabel and infile functions and have not seen anything much helpful. Can anyone see where I am going wrong and why these errors are popping up?
Does anyone have any suggestion on how to save the paths locally (to a variable perhaps) so that I can pull them later? I've had limited to no success so far.
Thank You.
Below is my code:
import os
from Tkinter import *
from tkinter import filedialog
content = 'apple'
file_path = 'squarebot'
#FUNCTIONS
def browsefunc(): #browse button to search for files
filename = filedialog.askopenfilename()
infile = open(filename, 'r')
content = infile.read()
pathadd = os.path.dirname(filename)+filename
pathlabel.delete(0, END)
pathlabel.insert(0, pathadd)
return content
def browsefunc2(): #browse button to search for files
filename2 = filedialog.askopenfilename()
infile = open(filename2, 'r')
content = infile.read(filename2)
pathadd = os.path.dirname(filename2)+filename2
pathlabel.delete(0, END)
pathlabel.insert(0, pathadd)
return content
def process_file(content): #process reconciliation code
print(content)
def directoryname():
directoryname = filedialog.askdirectory() # pick a folder
#GUI
root = Tk()
root.title('Reconciliation Converter')
root.geometry("598x150")
mf = Frame(root)
mf.pack()
f1 = Frame(mf, width=600, height=250) #file1
f1.pack(fill=X)
f2 = Frame(mf, width=600, height=250) #file2
f2.pack(fill=X)
f3 = Frame(mf, width=600, height=250) #destination folder
f3.pack(fill=X)
f4 = Frame(mf, width=600, height=250) #reconcile button
f4.pack()
file_path = StringVar
Label(f1,text="Select file 1 (Only txt files)").grid(row=0, column=0, sticky='e') #file1 button
entry = Entry(f1, width=50, textvariable=file_path)
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Label(f2,text="Select file 2 (Only csv files)").grid(row=0, column=0, sticky='e') #file2 button
entry = Entry(f2, width=50, textvariable=file_path)
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Label(f3,text="Select Your Destination Folder").grid(row=0, column=0, sticky='e') #destination folder button
entry = Entry(f3, width=50, textvariable=directoryname)
entry.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25)
Button(f1, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#file1 button
Button(f2, text="Browse", command=browsefunc2).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#file2 button
Button(f3, text="Browse", command=browsefunc).grid(row=0, column=27, sticky='ew', padx=8, pady=4)#destination folder button
Button(f4, text="RECONCILE NOW", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=10)#reconcile button
root.mainloop()