FAQ:  What happens when the Open or Save dialog is cancelled within the pythonaddins module?

相关信息
Article ID: 40535
Software:
ArcGIS for Desktop Advanced 10.1
ArcGIS for Desktop Standard 10.1
ArcGIS for Desktop Basic 10.1
Platforms:
Windows XP, Server 2003, Vista, Server 2008, Windows 7

问题描述
What happens when the Open or Save dialog is cancelled within the pythonaddins module?
已邀请:

EsriSupport

赞同来自:

解决方案
The cancel operation returns the 'NoneType' object (None). This is similar to the boolean objects of True and False.



In addition to the dialog function, there is the MessageBox function which has a variety of button options based upon its defined type. This function returns a string value representing the message button pressed (i.e., 'Cancel', 'Ignore', 'Continue').


An 'if' clause can be used to determine the next steps within the Python code after a button is clicked. For example:
import arcpy
import pythonaddins

class Open(object):
"""Implementation for Dialog_addin.OpenButton (Button)"""
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
#Define the map document and data frame
mxd = arcpy.mapping.MapDocument("CURRENT")
df = arcpy.mapping.ListDataFrames(mxd,"*")[0]

#Call the open dialog
openValue = pythonaddins.OpenDialog("Open Dialog", False, r"C:\temp", "Open")
#Make sure dialog was not canceled.
if openValue != None:
#Create the layer
rasterLayer = arcpy.mapping.Layer(openValue)
#Add the layer to the map document
arcpy.mapping.AddLayer(df, rasterLayer,"TOP")
# Refresh map document
arcpy.RefreshActiveView()
arcpy.RefreshTOC()
del mxd, df, openValue
else:
pythonaddins.MessageBox("No data will be added to the map document.", "Warning Message", 0)
del mxd, df, openValue




创建及修改时间
Created: 8/24/2012

Last Modified: 7/10/2013
原文链接
http://support.esri.com/en/kno ... 40535

要回复问题请先登录注册