pythonaddins 模块包括用于支持 Python 加载项的函数。
函数 | 说明 | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
OpenDialog({title}, {multiple_selection}, {starting_location}, {button_caption}, {filter}, {filter_label}) | 打开对话框以选择一个或多个 GIS 数据集。此函数返回所选数据集的完整路径。如果选择多个数据集,将返回一份完整路径列表。不能过滤输入数据集(例如,没有仅针对点要素类的过滤器)。
| ||||||||||||
SaveDialog({title}, {name_text}, {starting_location}, {filter}, {filter_label}) | 打开对话框以保存数据。此函数返回要保存的数据集的完整路径。
| ||||||||||||
GPToolDialog(toolbox, tool_name) | 打开地理处理工具对话框。
| ||||||||||||
MessageBox(message, title, {mb_type}) | 显示消息框。此函数返回表示按下的消息按钮的字符串值。
| ||||||||||||
GetSelectedTOCLayerOrDataFrame() | 返回内容列表中的所选图层或数据框。 | ||||||||||||
GetSelectedCatalogWindowPath() | 在目录窗口中返回所选项目的完整路径。 | ||||||||||||
ProgressDialog() | 返回 ProgressDialog 对象。当您进入 with 块时,进度对话对象会自动可见并在您退出时消失。请参阅下面的示例代码片段。 属性为:
|
mb_type 代码 | 消息框类型 |
---|---|
0 | 仅确定 |
1 | 确定/取消 |
2 | 中止/重试/忽略 |
3 | 是/否/取消 |
4 | 是/否 |
5 | 重试/取消 |
6 | 取消/重试/继续 |
此加载项按钮使用 OpenDialog() 选择一组图层文件,并将每个图层都添加到所选数据框中。
import arcpy
import pythonaddins
class AddLayers(object):
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
layer_files = pythonaddins.OpenDialog('Select Layers', True, r'C:\GISData', 'Add')
mxd = arcpy.mapping.MapDocument('current')
df = pythonaddins.GetSelectedTOCLayerOrDataFrame()
if not isinstance(df, arcpy.mapping.Layer):
for layer_file in layer_files:
layer = arcpy.mapping.Layer(layer_file)
arcpy.mapping.AddLayer(df, layer)
else:
pythonaddins.MessageBox('Select a data frame', 'INFO', 0)
此加载项按钮可打开地理处理工具。
import arcpy
import pythonaddins
class OpenGPTool(object):
def __init__(self):
self.enabled = True
self.checked = False
def onClick(self):
pythonaddins.GPToolDialog(r'C:\MyTools\WaterStudy.tbx', 'GroundWaterYield')
进度对话示例:在 ArcGIS for Desktop 的 Python 窗口中进行测试。
import pythonaddins
with pythonaddins.ProgressDialog as dialog:
dialog.title = "Progress Dialog"
dialog.description = "Copying a large feature class."
dialog.animation = "File"
for i in xrange(100):
dialog.progress = i
time.sleep(0.125)
if dialog.cancelled:
raise Exception("Ooops")
仅允许打开文本文件。
import os
class MyValidator(object):
def __str__(self):
return "Text files(*.txt)"
def __call__(self, filename):
if os.path.isfile(filename) and filename.lower().endswith(".txt"):
return True
return False
filename = pythonaddins.OpenDialog(r"c:\files", filter=MyValidator())