error: global name 'Primary_OH' is not defined

4353
43
Jump to solution
12-27-2019 11:14 AM
RickHawkins
New Contributor II

I am trying to run my program and when i get to this part of the program

I get the error that global name 'Primary_OH is not defined. Can someone look at my code and tell what i need to do to fix this.

Thanks

Tags (1)
0 Kudos
43 Replies
RickHawkins
New Contributor II

also, after i removed line 88 the program ran fine but all of the feeders were still showing on the map. What do i have to do after the program runs lines 86-89 to not show feeders that are not the one selected such as 2050. This will be a variable that the user selects from a list.

0 Kudos
RickHawkins
New Contributor II

Javier,

This is my partially completed python script:

from tkinter import *
from tkinter import messagebox
import Tkinter, arcpy

mxd = arcpy.mapping.MapDocument("C:/GISMAPS/LUS Map.mxd")
#mxd = arcpy.mapping.MapDocument("CURRENT")

#reset (overwrite) MakeFeatureLayer_management. This is needed so you can run the program more than once.
arcpy.env.overwriteOutput = True 
#delete the layer
#arcpy.Delete_managment 

#set variables for when you run the section of program that set the layers that should be on
geodatabase = "C:\gismaps\LusData.mdb"
dataset = "ElectricData"
feature_class = "PriOHElectricLineSegment"

#Create main window
window = Tk()
window.title("Feeders Selection")
window.geometry("680x500")
window.iconbitmap('C:/GISMAPS/lus.ico')


#Define the frame for the feeders to be displayed
frame = Frame(window)
frame.grid(row=1, column=1, padx=15, pady=5)
listNodes = Listbox(frame, width=10, height=10, font=("Helvetica", 10))
listNodes.pack(side="left", fill="y")

#Define the scrollbar in frame window
scrollbar = Scrollbar(frame, orient="vertical")
scrollbar.config(command=listNodes.yview)
scrollbar.pack(side="right", fill="y")
listNodes.config(yscrollcommand=scrollbar.set)


#Add the selection of all feeders
for item in [2050, 2051, 2052,
             3050, 3051, 3052, 3550,
             4050, 4051, 4052,
             5050, 5051, 5052]:
    listNodes.insert(END, item)
    feeder = listNodes.get(ACTIVE)


#Define all of the checkboxes
prioh_var = IntVar()
priug_var = IntVar()
secoh_var = IntVar()
secug_var = IntVar()
fuse_var = IntVar()
switch_var = IntVar()
sp_var = IntVar()
cap_var = IntVar()
rec_var = IntVar()
pinhook_var = IntVar()
mall_var = IntVar()
peck_var = IntVar()
pri_oh = Checkbutton(window, text = "Primary Overhead", variable = prioh_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=2, y=175)
pri_ug = Checkbutton(window, text = "Primary Underground", variable = priug_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=12, y=200)
sec_oh = Checkbutton(window, text = "Secondary Overhead", variable = secoh_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=8, y=225)
sec_ug = Checkbutton(window, text = "Secondary Underground", variable = secug_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=18, y=250)
fuse = Checkbutton(window, text = "Fuse", variable = fuse_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=400, y=10)
switch = Checkbutton(window, text = "Switch", variable = switch_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=405, y=35)
servicepoint = Checkbutton(window, text = "Service Point", variable = sp_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=420, y=88)
capacitor = Checkbutton(window, text = "Capacitor", variable = cap_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=412, y=60)
recloser = Checkbutton(window, text = "Recloser", variable = rec_var,onvalue = 1, offvalue = 0, height=2, width = 20).place(x=409, y=87)
pinhook = Checkbutton(window, text = "Pinhook", variable = pinhook_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=175, y=15)
mall = Checkbutton(window, text = "Mall", variable = mall_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=165, y=45)
peck = Checkbutton(window, text = "Peck", variable = peck_var, state = DISABLED, onvalue = 1, offvalue = 0, height=2, width = 20).place(x=165, y=75)


#Commit all of the options

def commit():
    feeder = listNodes.get(ACTIVE)
    #print feeder
    #my_label = Label(window, text=prioh_var.get()).pack()
    
#check to see which inputs have been selected
#Primary Overhead
    if prioh_var.get() == 1:
        print("pri_oh = 1") 
	# show primary overhead feeder where feederid = feeder -ie: [FEEDERID] = "2050"
        print "Primary_OH"
        print feeder
		
        
        layer_name = "Primary OH"  
        primary_oh = arcpy.mapping.ListLayers(mxd, layer_name)[0] 
        primary_oh.definitionQuery = "[FEEDERID] = '2050'"  
        
        #Primary_OH_path = r"{0}\{1}\{2}".format(geodatabase, dataset, feature_class) 
        #Primary_OH = arcpy.MakeFeatureLayer_management(Primary_OH_path, "Primary_OH_layer2") 

    else:
        print("pri_oh = 0")
		# do not display primary overhead feeder for [FEEDERID] = "2050"
		
        
#Primary Underground		
	if priug_var.get() == 1:
		print("pri_ug = 1")
		print feeder
		
		layer_name = "Primary UG"  
        primary_ug = arcpy.mapping.ListLayers(mxd, layer_name)[0] 
        primary_ug.definitionQuery = "[FEEDERID] = '2050'"  

    #else:
        #print("pri_ug = 0")
        # do not display primary underground feeder for [FEEDERID] = "2050"
        
    


	
#reset all variables  
#def reset():



  
#Button to run the commit command    
Button(window, text="commit", command=commit).place(x=300, y=350)

#Button to run the reset command
#Button(window, text="reset", command=reset).place(x=400, y=350)

#Quit the program
Button(window, text="Quit", command=window.destroy).place(x=500, y=350)


                     
window.mainloop()

The program ran fine but all of the feeders were still showing on the map. What do i have to do to alter the script so that when the program runs lines 90-92 the feeders that are not the one selected (such as 2050) are not shown?. This field will be a variable that the user selects from a list so i need to allow this to be a variable

Thanks for your help

0 Kudos
Javier_AntonioEscudero
Esri Contributor

Hi Rick

looks pretty good!

Adds the following command to end of the script to save changes to the map.

mxd.save()

Regards!

0 Kudos
RickHawkins
New Contributor II

i added mxd.save() and got the following:

I pretty sure  that i have access to the file.

0 Kudos
Javier_AntonioEscudero
Esri Contributor

According to Joe Borgione mentioned, Use "r" before a variable that include a path, 

Use

mxd = arcpy.mapping.MapDocument(r"C:/GISMAPS/LUS Map.mxd")

instead of

mxd = arcpy.mapping.MapDocument("C:/GISMAPS/LUS Map.mxd")
0 Kudos
RickHawkins
New Contributor II

I will check on the permissions. By adding mxd.save() to the script does this make a permanent save to the file? 

0 Kudos
RickHawkins
New Contributor II

I added the new path and still got the same answer

0 Kudos
Javier_AntonioEscudero
Esri Contributor

Try executing the Python Script as administrator to test if is a permissions issue.

0 Kudos
RickHawkins
New Contributor II

ran as admin and still received the error. I am sorry for taking up so much of your time. I do appreciate it

0 Kudos
Javier_AntonioEscudero
Esri Contributor

Adds the mxd.save() command before quit the full program , may be in  line 115, inside commit function. Don't forget indentation.

#Commit all of the options
def commit():

   # -------------------#
   # All your code here # 
   # -------------------#
   mxd.save()
   return       
0 Kudos