使用Python在ArcGIS中自动刷新

使用Python在ArcGIS中自动刷新,python,arcgis,Python,Arcgis,我正在尝试为ArcMap创建一个“自动刷新”工具,以刷新数据帧。我相信版本10有一个附加组件,你可以为此下载。。但是,我们正在运行10.1,没有这样的工具 EDITwxPython的计时器应该可以工作,但是在arc中使用wx是很棘手的。下面是代码当前的样子: import arcpy import pythonaddins import os import sys sMyPath = os.path.dirname(__file__) sys.path.insert(0, sMyPath) W

我正在尝试为ArcMap创建一个“自动刷新”工具,以刷新数据帧。我相信版本10有一个附加组件,你可以为此下载。。但是,我们正在运行10.1,没有这样的工具

EDITwxPython的计时器应该可以工作,但是在arc中使用wx是很棘手的。下面是代码当前的样子:

import arcpy
import pythonaddins
import os
import sys
sMyPath = os.path.dirname(__file__)
sys.path.insert(0, sMyPath)

WATCHER = None

class WxExtensionClass(object):
    """Implementation for Refresher_addin.extension (Extension)"""
    _wxApp = None
    def __init__(self):
        # For performance considerations, please remove all unused methods in this class.
        self.enabled = True
    def startup(self):
        from wx import PySimpleApp
        self._wxApp = PySimpleApp()
        self._wxApp.MainLoop()
        global WATCHER
        WATCHER = watcherDialog()


class RefreshButton(object):
    """Implementation for Refresher_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        if not WATCHER.timer.IsRunning():
            WATCHER.timer.Start(5000)
        else:
            WATCHER.timer.Stop()

class watcherDialog(wx.Frame):
    '''Frame subclass, just used as a timer event.'''
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "timer_event")
        #set up timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)

    def onTimer(self, event):
        localtime = time.asctime( time.localtime(time.time()) )
        print "Refresh at :", localtime
        arcpy.RefreshActiveView()

    app = wx.App(False)

你会注意到里面有PySimpleApp的东西。我是从Cederholm的演讲中得到的。我想知道我是否误解了什么。我应该为扩展创建一个完全独立的加载项吗?然后,用我需要的代码创建我的工具栏/工具栏加载项?我这样问是因为我看不到下面代码中引用的PySimpleApp,也看不到启动覆盖方法中从wx导入的任何内容。。。我认为这是必要的/这一切的重点。我非常感谢你的帮助。请让我知道您在我的代码中看到了什么。

您可以使用或方法。只需添加一个

就可以了。你不能用你正在尝试的方式来做这件事,因为
time.sleep
会阻塞并锁定整个应用程序。ArcGIS中的Python插件是非常新的东西,还有很多功能尚未实现。其中之一是某种更新或计时器事件,就像在.NET和ArcObjects中一样。在这种情况下,您可能会考虑使用threading.Thread和threading.Event,但在Python加载项环境中,与线程无关的内容将不起作用。至少我不能让它工作。所以我在这种情况下所做的就是使用wxPython和Timer类。如果加载项设置正确,下面的代码将起作用

import time
import os, sys
import wx
import arcpy

mp = os.path.dirname(__file__)
sys.path.append(mp)

WATCHER = None

class LibLoader1(object):
    """Extension Implementation"""
    def __init__(self):
        self.enabled = True

    def startup(self):
        global WATCHER
        WATCHER = watcherDialog()

class ButtonClass5(object):
    """Button Implementation"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
        if not WATCHER.timer.IsRunning():
            WATCHER.timer.Start(5000)
        else:
            WATCHER.timer.Stop()

class watcherDialog(wx.Frame):
    '''Frame subclass, just used as a timer event.'''
    def __init__(self):
        wx.Frame.__init__(self, None, -1, "timer_event")
        #set up timer
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, self.timer)

    def onTimer(self, event):
        localtime = time.asctime( time.localtime(time.time()) )
        print "Refresh at :", localtime
        arcpy.RefreshActiveView()

    app = wx.App(False)
使用工具栏和按钮类创建扩展插件。覆盖扩展的
启动
方法,如上所示。这将创建一个带有计时器的Frame子类的实例。然后,无论何时单击工具栏上的按钮,计时器都将打开或关闭。计时器参数以毫秒为单位,因此显示的代码将每5秒刷新一次

您可以在外接程序中阅读有关使用wxPython的更多信息。特别注意MCederholm的帖子,比如关于打印声明不起作用的帖子

编辑 代码使用加载项扩展类的
启动
方法重写。这个方法应该在Arcmap启动时运行,但从您的评论来看,这个启动方法在启动时无法运行。如果您没有正确创建您的加载项,这是可能的,但它在我的测试中对我来说很好。如果继续获取“AttributeError:'NoneType'对象没有属性'timer',则更改button类的
onClick
方法,如下所示:

def onClick(self):

    if WATCHER is None:
        global WATCHER
        WATCHER = watcherDialog()

    if not WATCHER.timer.IsRunning():
        WATCHER.timer.Start(5000)
    else:
        WATCHER.timer.Stop()

前3行检查以确保WATCHER变量已设置为
watcherDialog
的实例,并且尚未设置为
None
。不知道你的启动方法为什么没有运行,但希望这能帮你解决问题。

你需要某种并发性,time.sleep会阻止并锁定Arcmap。太棒了!您提供的代码看起来很棒。。非常感谢。我一直在努力让wx在ArcMap的python窗口中导入。不确定这是否与我安装的wxPython有关。我已经在ArcGIS10.1\lib中专门安装了它,但是,在尝试导入wx时总是会出现错误。我将观看Mark Cederholm的2012年开发峰会演示,我从您引用的链接中获得了该演示。(见我的另一个问题:)我得到的错误还有:运行时错误回溯(最近一次调用):文件“”,第1行,在文件“C:\Python27\ArcGIS10.1\lib\site packages\wx-2.8-msw-unicode\wx_init_uuu.py”的第45行,在from wx.\u core import*文件“C:\Python27\ArcGIS10.1\lib\site packages\wx-2.8-msw-unicode\wx_ucore.py”的第4行,在导入core ImportError中:DLL加载失败:%1不是有效的Win32应用程序。尝试在python窗口中导入wx,或使用启动方法在加载项扩展中导入wx时,是否会出现此错误?你必须严格按照塞德霍尔姆的指示去做,否则你会遇到像这样的各种错误。在带有启动方法的扩展中导入wx,您应该已经上路了。好的-终于让扩展显示在Arc中,按钮似乎想要播放得很好。我得到一个回溯:第29行,在onClick if not WATCHER.timer.IsRunning()中:AttributeError:'NoneType'对象没有属性'timer'。您需要转到加载项管理器并加载加载项。然后重新启动Arcmap。这样做一次,它就会自动加载到那台机器上。