NameError: global name is not defined when Python script is run from ArcMap.

8796
4
05-10-2019 01:58 PM
DaleShearer
Occasional Contributor

Continuing on in learning Python as I go I am at the next issue.  I have created a Python toolbar add-in with a button for use in ArcMap.

Start up ArcMap, toolbar is there, select button and a menu I created in Python is displayed.  It contains radiobuttons and comboboxes.

On the reset button I want to clear the radiobuttons, clear the selected item from the combobox so it is not selected-blue, and set the combobox back to the first item in the list.

When I run the script from PyScripter the menu appears and I can clear and reset everything so the menu looks like it just loaded.

When I start ArcMap the Python toolbar appears, select the button, menu appears, select a radiobutton, select an item from a combobox, that all works.

When I select the reset button I get the following error whether it is a radiobutton or a combobox:

I have the python window open in ArcMap so I can see what is happening.

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\ArcGIS10.5\Lib\lib-tk\Tkinter.py", line 1542, in __call__
return self.func(*args)
File "D:\Applications\Python\Layers.py", line 63, in reset
RB1.selection_clear = var.set(0)
NameError: global name 'RB1' is not defined

This is beyond my knowledge at this point, I did try to set a global variable with no change to the error message.

These are my import statements for the script:

from tkinter import *
from tkinter import ttk
import tkFont
import arcpy
from arcpy import env
import sqlite3
import pythonaddins

I am not putting in all the code, for now it is all radiobuttons and comboboxes, very repetitive.

This is the code I have concerning 'RB1':

def reset():
     RB1.selection_clear = var.set(0)

RB1 = Radiobutton(root, text="Single Map",padx = 5, variable=var, value=1, font="Arial 9") #, state=NORMAL).place(x=10,y=60)
RB1.pack()
RB1.place(x=10,y=60)
RB1.config(state=NORMAL)

Button(root, text='Reset',width=20,bg='brown',fg='white', font="Arial 8 bold", command=reset).place(x=39,y=237)

As this is my first Python script ever, and for use within ArcMap, I am sure there are a few things I am missing.

Thank you for looking at my question.

0 Kudos
4 Replies
RandyBurton
MVP Alum

I haven't worked a lot with add-ins,  but I have experienced issues with script tools and Python toolboxes not working well with global variables.  I think it has to do with how/where the global is declared and function calls.  This link may give some  insight, but won't directly answer your question: Python toolbox tool objects do not remember instance variables between function calls?

0 Kudos
DanPatterson_Retired
MVP Emeritus

As Richard said in this thread  https://community.esri.com/message/379045?commentID=379045#comment-379045 

Tk isn't supported in ArcMap.

A search of other grief filled threads on GeoNet are available for perusal... ArcMap TkInter … will give you many old ones.  some glimmers of hope, a few workarounds, but everything is going to be situation specific.  It is just best not to let the two play together in the first place

0 Kudos
DaleShearer
Occasional Contributor

Thanks for the replies, as Tkinter does not work well with ArcMap, what advice-direction can you give me as how to develop a menu in Python for use in ArcMap, basically where to start at again.

0 Kudos
geraldwarp
New Contributor

In most cases, this error is triggered when Python sees a variable name (Global or Local) and doesn't know what it's for. These errors can happen if you forget to initialize a variable , if you misspell a variable, or if you misspell a reserved word such as "True". Before you use the global variable in your function for reading, it must be first initialized somewhere: either outside of the function or inside it.

 

0 Kudos