1

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.

  1. 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?

  2. 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()
A. Blackmagic
  • 233
  • 1
  • 3
  • 9

1 Answers1

1

Note that this code can be optimized, but this should get you going:

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
    file_path1.set(pathadd)
    return content

def browsefunc2(): #browse button to search for files
    filename2 = filedialog.askopenfilename()
    infile = open(filename2, 'r')
    content = infile.read()
    pathadd = os.path.dirname(filename2)+filename2
    file_path2.set(pathadd)
    return content

def browsefunc3(): #browse button to search for files
    directory = filedialog.askdirectory(initialdir='.')
    directoryname.set(directory)
    return content

def process_file(content): #process reconciliation code
    print('------------------------------')
    print(file_path1.get())
    print(file_path2.get())
    print(directoryname.get())

#GUI

root = Tk()

root.title('Reconciliation Converter')
root.geometry("698x150")

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_path1 = StringVar()
file_path2 = StringVar()
directoryname = StringVar()

Label(f1,text="Select file 1 (Only txt files)").grid(row=0, column=0, sticky='e') #file1 button
entry1 = Entry(f1, width=50, textvariable=file_path1)
entry1.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
entry2 = Entry(f2, width=50, textvariable=file_path2)
entry2.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
entry3 = Entry(f3, width=50, textvariable=directoryname)
entry3.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=browsefunc3).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()
SuperKogito
  • 2,998
  • 3
  • 16
  • 37
  • Why is it that when i execute the code, both file_path1.get() and file_path2() are appended to directoryname.get()? Thanks for the help otherwise! I really appreciate it. – A. Blackmagic Oct 06 '17 at 14:33
  • I am not sure that I understand, what do you mean by "appended to"? All what this code does is print out the paths retrieved from the entries. – SuperKogito Oct 06 '17 at 18:07
  • When I actually fixed it. In your code you were using pathadd = os.path.dirname(filename2)+filename2 and pathadd = os.path.dirname(filename)+filename which printed the directory path, and then appended the filepath after the directory path, creating a path that doesnt exist. i simply switched it to os.path.join (filename, filename) which has a "smart" join feature that joins both paths, while removing redundancies. – A. Blackmagic Oct 06 '17 at 19:43
  • That was your original code, but join is also an option. However you might want to check this: https://stackoverflow.com/questions/28373288/get-file-path-from-askopenfilename-function-in-tkinter – SuperKogito Oct 06 '17 at 20:28