Tkinter name error (not defined) on user input

I am getting this error:

Traceback (most recent call last):
  File "C:\Users\Suhail\Desktop\NLP project\NLP project.py", line 12, in <module>
    submit_button = tk.Button(root, text="Submit", command=submit)
NameError: name 'submit' is not defined

I am writing this code for an NLP project which takes PDF files written in Gujrati languages and then process them in backend to summarize the texts in English and gives the option to download the summrized file

here is my source code:

import tkinter as tk
from tkinter import filedialog
import nltk
from nltk.corpus import stopwords
# Create the user interface
root = tk.Tk()
root.title("Document Summarization")
# Create the input field
input_field = tk.Entry(root)
input_field.pack()
# Create the submit button
submit_button = tk.Button(root, text="Submit", command=submit)
submit_button.pack()
# Create the output label
output_label = tk.Label(root)
output_label.pack()
# Define the submit function
def submit():
    # Get the input file
    file = filedialog.askopenfile(mode="rb", filetypes=[("PDF files", "*.pdf")])
    # Read the file contents
    text = file.read().decode("utf-8")
    # Tokenize the text
    tokens = nltk.word_tokenize(text)
    # Remove stop words
    stop_words = set(stopwords.words("gujarati"))
    tokens = [token for token in tokens if token not in stop_words]
    # Create a bag-of-words model
    bow = nltk.FreqDist(tokens)
    # Extract the most important words
    important_words = bow.most_common(10)
    # Generate a summary
    summary = " ".join([word[0] for word in important_words])
    # Display the summary
    output_label.config(text=summary)
# Start the main loop
root.mainloop()

Upvotes: 0

Views: 32

Answers (1)

Marin Nagy
Marin Nagy

Reputation: 257

You make a reference in line 12 to a function that you define in line 18. When the compiler reads your code it looks for a submit function that doesn't exist yet. In order to fix your error you have to create the submit function before using it, so simply move it before line 12:

import tkinter as tk
from tkinter import filedialog
import nltk
from nltk.corpus import stopwords

# Create the user interface
root = tk.Tk()
root.title("Document Summarization")

# Define the submit function
def submit():
    # Get the input file
    file = filedialog.askopenfile(mode="rb", filetypes=[("PDF files", "*.pdf")])
    # Read the file contents
    text = file.read().decode("utf-8")
    # Tokenize the text
    tokens = nltk.word_tokenize(text)
    # Remove stop words
    stop_words = set(stopwords.words("gujarati"))
    tokens = [token for token in tokens if token not in stop_words]
    # Create a bag-of-words model
    bow = nltk.FreqDist(tokens)
    # Extract the most important words
    important_words = bow.most_common(10)
    # Generate a summary
    summary = " ".join([word[0] for word in important_words])
    # Display the summary
    output_label.config(text=summary)

# Create the input field
input_field = tk.Entry(root)
input_field.pack()

# Create the submit button
submit_button = tk.Button(root, text="Submit", command=submit)
submit_button.pack()

# Create the output label
output_label = tk.Label(root)
output_label.pack()

# Start the main loop
root.mainloop()

Upvotes: 0

Related Questions