I am trying to have the file directory open when a certain button is pressed in the tkinter GUI, but the directory automatically opens when I run the program. Addittionally, if I press cancel when in the file directory, my program freezes up and I have to close the program, and I'm not sure why this is.

I tried putting all of the tkinter related coding in a seperate file, but when I tried to call a method from that file, it would open up the tkinter GUI twice, so that didn't work and I wasn't able to solve the problem, so I thought combining the two would make it a little easier. I was able to have the tkinter GUI stop appearing twice, but now I am at this dilemma. I tried using the debugger that comes with spyder, but it wasn't able to help much besides showing me why the tkinter GUI kept appearing twice.

```
import os
import PyPDF2
import pandas
import webbrowser
import tkinter as tk
from tkinter import ttk
from tkinter import filedialog
from nltk.tokenize import RegexpTokenizer
from nltk.corpus import stopwords
from nltk.stem import PorterStemmer
from nltk.stem.wordnet import WordNetLemmatizer
from nltk.tokenize import word_tokenize

#Creats the GUI that will be used to select inputs#
window = tk.Tk()
window.geometry("300x300")
window.resizable(0, 0)
window.title("Word Frequency Program")

#Allows user to select PDF to use in program#
def select_PDF():
    filename = filedialog.askopenfilename(initialdir = "/", title = "Select file", filetypes = (("pdf files", "*.pdf"), ("all files", "*.*")))
    return filename
button1 = ttk.Button(window, text = "Select File", command = select_PDF)
button1.grid()

#Quits out of the program when certain button clicked#
button3 = ttk.Button(window, text = "Quit", command = window.quit)
button3.grid()

#Loads in PDF into program#
filepath = select_PDF()
PDF_file = open(filepath, 'rb')
read_pdf = PyPDF2.PdfFileReader(PDF_file)

#Determines number of pages in PDF file and sets the document content to 'null'#
number_of_pages = read_pdf.getNumPages()
doc_content = ""

#Extract text from the PDF file#
for i in range(number_of_pages):
    page = read_pdf.getPage(0)
    page_content = page.extractText()
    doc_content += page_content

#Method that a pdf that is read into the program goes through to eliminate any unwanted words or symbols#
def preprocess(text):
    #Filters out punctuation from paragraph witch becomes tokenized to words and punctuation#
    tokenizer = RegexpTokenizer(r'\w+')
    result = tokenizer.tokenize(text)

    #Makes all words lowercase#
    words = [item.lower() for item in result]

    #Removes all remaining tokens that are not alphabetic#
    result = [word for word in words if word.isalpha()]

    #Imports stopwords to be removed from paragraph#
    stop_words = set(stopwords.words("english"))

    #Removes the stop words from the paragraph#
    filtered_sent = []
    for w in result:
        if w not in stop_words:
            filtered_sent.append(w)

    #Return word to root word/chop-off derivational affixes#
    ps = PorterStemmer()
    stemmed_words = []
    for w in filtered_sent:
        stemmed_words.append(ps.stem(w))

    #Lemmatization, which reduces word to their base word, which is linguistically correct lemmas#
    lem = WordNetLemmatizer()
    lemmatized_words = ' '.join([lem.lemmatize(w,'n') and lem.lemmatize(w,'v') for w in filtered_sent])

    #Re-tokenize lemmatized words string#
    tokenized_word = word_tokenize(lemmatized_words)
    return tokenized_word

#Turns the text drawn from the PDF file into data the remaining code can understand#
tokenized_words = preprocess(doc_content)

#Determine frequency of words tokenized + lemmatized text#
from nltk.probability import FreqDist
fdist = FreqDist(tokenized_words)
final_list = fdist.most_common(len(fdist))

#Organize data into two columns and export the data to an html that automatically opens#
df = pandas.DataFrame(final_list, columns = ["Word", "Frequency"])
df.to_html('word_frequency.html')
webbrowser.open('file://' + os.path.realpath('word_frequency.html'))

window.mainloop()
window.destroy()

```

The tkinter GUI should pop up on its own without the file directory appearing UNTIL you press a button in the GUI. The program also should not crash when you press cancel in the file directory.