0

Please help me with this error. I am very confused as to why I cannot reference and obtain the contents of the entry. If someone can please help, it will be very greatly appreciated. If you need anymore context on what the problem is, please leave a comment.

When I run the program I get an error which says

self.file_location = file_location_entry.get()
NameError: name 'file_location_entry' is not defined

I am confused for getting this error since I have defined this variable below inside of the class in the main initializer function.

# Imports
import tkinter as tk
from tkinter import ttk

# ==================================================================================================
class GUI():

    # Global variable for file location of software to scan
    global file_location

# ==================================================================================================
    # Button Functions for Windows

    # Function used to open other window that will be used for the script based attacks
    def script_attack_window(self):
        pass

    # Close Window Button Command
    def close_window(self, window):
        return window.quit

    # Start Audit Button Command
    def start_audit(self):
        #function to start the security audit
        pass

    # Display Text To Textbox Command
    def display_text_to_textbox(self, text):
        self.display_textbox.insert(tk.END, text)

    def set_variable(self):
        self.file_location = self.file_location_entry.get()
        self.display_text_to_textbox(self.file_location)

    # Window for entering default location for auditing tool
    def default_location_auditing_tool_window(self):
        pass

# ==================================================================================================
    # Main Window Function

    # Rescource for using frames to organize the window better
    # https://stackoverflow.com/questions/2261191/how-can-i-put-2-buttons-next-to-each-other
    # https://www.tutorialspoint.com/python/tk_frame.htm
    # https://realpython.com/python-gui-tkinter/
    # https://tkdocs.com/tutorial/

    # This will be used to display the main menu when called
    def __init__(self):

        # Initializing a new window
        root = tk.Tk()
        root.title("Main Menu")
    # Making frames
        canvas = tk.Frame()
        blocked_space = tk.Frame(canvas)

        # Initializing the window settings
        root.attributes('-type', 'dialog')

#=======================Buttons=========================

        # Button to start the autiting software
        auditing_button = tk.Button(master=canvas, text="Audit", command=self.start_audit())

        # Button to close the window
        close_button = tk.Button(master=canvas, text="Close", command=self.close_window(root))

        # Button to open script page
        script_button = tk.Button(master=canvas, text="Script Attack", command=self.script_attack_window())

        # Button to set the file variable
        file_enter_button = tk.Button(master=canvas, text="Enter File Path", command=self.set_variable())

        # Textbox for getting user input for file location
        self.file_location_entry= tk.Entry(master=canvas)

        # TextBox for displaying the text
        self.display_textbox = tk.Text(master=canvas)

        # Plotting the buttons and textboxes and entry on the grid
        canvas.grid(column=0, row=0)

        auditing_button.grid(row=3, column=1)
        auditing_button.columnconfigure(1, weight=1)

        close_button.grid(row=3, column=0)
        close_button.columnconfigure(0, weight=1)

        script_button.grid(row=3, column=2)
        script_button.columnconfigure(2, weight=1)

        file_enter_button.grid(column=0, row=0)

        self.file_location_entry.grid(column=0, row=1, columnspan=3, sticky="ewn")

        self.display_textbox.grid(column=3, row=1)


        # Starting the window mainloop
        root.mainloop()

# ==================================================================================================

# ---------------------------------------------------------------------------------------------------

x = GUI()
~               ```
acw1668
  • 40,144
  • 5
  • 22
  • 34
  • That error message doesn't match any line in the code you posted (it's lacking a `self.`). Did you perhaps forget to save some changes to the file? – jasonharper Nov 15 '20 at 07:19
  • Your problem is that you are calling the functions in your `command` arguments, which calls `set_variable` before you create the `Entry`, and throws the error. Get rid of the parenthesis in all of your `command` assignments. – OneMadGypsy Nov 15 '20 at 07:23

1 Answers1

0

I think that the problem is in the order in which you write the code.

Below I've rewritten your script in full OO mode, that's mean without the use

of the global variable and the double passage to assign to the text the value

of the entry variable and with the right sequence, first the app callbacks and

app variables, after GUI init, and finally the callbacks.

#!/usr/bin/python3
import sys
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


class App(tk.Tk):
    """Main Application start here"""
    def __init__(self):
        super().__init__()

        self.protocol("WM_DELETE_WINDOW", self.on_close)
        self.title("Simple App")
        #never use global...
        self.text = tk.StringVar()
        self.init_ui()

  
    def init_ui(self):
        
        w = ttk.Frame(self, padding=5)
       
        r = 0
        c = 0
        self.file_location_entry = ttk.Entry(w,
                                             justify=tk.LEFT,
                                             textvariable=self.text)
        self.file_location_entry.grid(row=r, column=c, sticky=tk.W+tk.E, padx=5, pady=5)

        r += 1
        self.display_textbox = tk.Text(w)
        self.display_textbox.grid(row=r, column=c, padx=5, pady=5)

        r = 0
        c = 1
        
        b = ttk.LabelFrame(self, text="", relief=tk.GROOVE, padding=5)

        bts = [("Audit", 1, self.start_audit, "<Alt-a>"),
               ("Enter File Path", 0, self.set_variable, "<Alt-e>"),
               ("Script Attack", 0, self.script_attack_window, "<Alt-s>"),
               ("Close", 0, self.on_close, "<Alt-c>")]

        for btn in bts:
            ttk.Button(b, text=btn[0], underline=btn[1], command = btn[2]).grid(row=r,
                                                                                column=c,
                                                                                sticky=tk.N+tk.W+tk.E,
                                                                                padx=5, pady=5)
            self.bind(btn[3], btn[2])
            r += 1
             
           
        b.grid(row=0, column=1, sticky=tk.N+tk.S)
        w.grid(row=0, column=0, sticky=tk.N+tk.W+tk.S+tk.E)

        
    def start_audit(self, evt=None):
        pass

    def script_attack_window(self, evt=None):
        pass

    def set_variable(self, evt=None):

        self.display_textbox.insert(tk.END, self.text.get())
        
    def on_close(self,evt=None):
        """Close all"""
        if messagebox.askokcancel(self.title(), "Do you want to quit?", parent=self):
            self.destroy()


def main():

    app = App()

    app.mainloop()


if __name__ == '__main__':
    main()           

    
    

enter image description here

1966bc
  • 1,148
  • 1
  • 6
  • 11