0

So basically, I am learning Python (therefore I am new, so be gentle lol). I work in IT and wanted to make a program that has all the basic stuff that I do everyday.

The main program opens up and shows a few options for tools and such. I wanted to add a To Do List to the options.

When my To Do list is called the GUI will appear, however, whenever the buttons are clicked, I get the NameError. I assume the main program just doesn't understand the defined functions that I have assigned to the buttons on the To Do list.

I am curious as to why. Of course I would love a solution, however, I am genuinely curious and interested as to why the interpreter doesn't see or "understand" the defined functions.

I called the To Do List using

toDoBtn = tk.Button(self, text = "To Do List",
                command=lambda: exec(open("ToDo.py").read()))

The error I get is

Traceback (most recent call last):
  File "C:\Users\brannon.harper\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
    return self.func(*args)
  File "<string>", line 49, in insertTask
NameError: name 'inputError' is not defined

I will add the beggining part of ToDo.py, however, I feel as if the issue is how I am calling the script, not how the script is written.



from tkinter import *

from tkinter import messagebox 


tasks_list = [] 

counter = 1

# Function for checking input error when 
# empty input is given in task field 
def inputError() : 


    if enterTaskField.get() == "" : 


        messagebox.showerror("Input Error") 

        return 0

    return 1


The ToDo.py script ends with

if __name__ == "__main__" : 

    # create a GUI window 
    gui = Tk() 
    #### I just design the GUI here.

    #### After it is designed It ends with 

    gui.mainloop() 

Thanks so much for your time, and this is my first post, so if I did something wrong or didn't follow the "standard entry" please correct me and let me know for the future!

  • 2
    Instead of exec'ing the file, place the stuff under `__main__` into a function and import and call that – SuperStormer Apr 01 '20 at 18:43
  • 2
    Where do you call ``inputError``? Just ``exec`` on the file doesn't cause the error you show. Note that you should ``import`` your code instead of using ``exec``, regardless. – MisterMiyagi Apr 01 '20 at 18:53
  • I call ```inputError``` in the external script (ToDo.py) that I am trying to open from the main program. In my main program I did ```import ToDo```. However, as @incarnadine suggested, I took a look at the linked post and documentation. And It seems that ```exec``` may not be the function for this. – Scriptslayer Apr 01 '20 at 18:58
  • 1
    @Scriptslayer Never use `exec` unless you really know what you are doing. You should make it a module that you import, or just put it in the same file. – Keith Apr 01 '20 at 19:01
  • Yeah, I think I am just going to add it like I added everything else. I was hoping to make this a learning experience. It's just hard to justify the time for something as simple as a To Do List lol. – Scriptslayer Apr 01 '20 at 19:11

1 Answers1

0

The function inputError isn't defined because exec can't perform any operation that would bind local variables such as importing, assigning variables, or function/class definitions etc.

This is explained more in this SO post, or in the documentation here

incarnadine
  • 658
  • 7
  • 19