1

I have a toolbar with two buttons. It's a crude toggle that looks at a text file containing a single character whose values are either 0 or 1. When the file contents are 0, then button1 ResumeExports() should show up as depressed and disabled while button2 PauseExports() is enabled and normal. When the contents of the file are 1, then the button2 should be active and button1 inactive--it is used as a switch to allow or disallow other processing to occur. The toggle works, and the file gets updated. How can I get the button status (checked/unchecked, enabled/disabled) on the interface to reflect that? Both buttons always show enabled and unchecked. I'm at 10.2.1.

import arcpy
import pythonaddins

class ResumeExports(object):
    """Implementation for PythonAddIns_addin.res_btn (Button)"""
    ctl_file = r"C:\Users\myusername\Documents\ArcGIS\switch.txt"
    def __init__(self):
        with open(ctl_file, 'r') as i:
            content = i.read(1)
        if content == '1':          # exports are allowed 
            self.enabled = False    # button should be inactive
            self.checked = True     # button should be depressed
        if content == '0': # exports are disallowed, resume button is active
            self.enabled = True
            self.checked = False
    def onClick(self):
        with open(ctl_file, "r+") as f:
            data = f.read()
            f.seek(0)
            f.write("1")
            f.truncate()       

class PauseExports(object):
    """Implementation for PythonAddIns_addin.stop_btn (Button)"""
    ctl_file = r"C:\Users\myusername\Documents\ArcGIS\switch.txt"
    def __init__(self):
        with open(ctl_file, 'r') as i:
            content = i.read(1)
        if content == '0':          # exports are paused
            self.enabled = False    # button should be inactive
            self.checked = True     # button should be depressed
        if content == '1': # exports are allowed, pause button is active
            self.enabled = True
            self.checked = False
    def onClick(self):
        ctl_file = r"C:\Users\myusername\Documents\ArcGIS\switch.txt"
        with open(ctl_file, "r+") as f:
            data = f.read()
            f.seek(0)
            f.write("0")
            f.truncate()
1

I re-wrote it to use a single button that appears depressed when external processing is turned off.

import arcpy
import pythonaddins

class PauseResumeExports(object):
    """Implementation for ExportControl_addin.button (Button)"""
    def __init__(self):
        ctl_file = r"C:\Users\myusername\Documents\ArcGIS\switch.txt"
        with open(ctl_file, 'r') as i:
            content = i.read(1)
        if content == '1':          # processing allowed, pause tool appears functional
            self.enabled = True     
            self.checked = False    
        if content == '0':          # processing not allowed to run, pause tool is disabled
            self.enabled = True
            self.checked = True

    def onClick(self): # pause exports; change "1" to "0"
        ctl_file = r"C:\Users\myusername\Documents\ArcGIS\switch.txt"
        # get current value of ctl_file... from _init_ button should already be set
        with open(ctl_file, 'r') as i:
            content = i.read(1)
        if content == '1':
            # exports are allowed to run; button press disables
            # 1) disable it (set to "0")
            with open(ctl_file, "r+") as f:
                data = f.read()
                f.seek(0)
                f.write("0")
                f.truncate()
                # 2) update button (set to checked)
                self.enabled = True     
                self.checked = True    
        if content == '0':
            # exports are administratively disabled; button press will enable
            # 1) disable it (set to "0")
            with open(ctl_file, "r+") as f:
                data = f.read()
                f.seek(0)
                f.write("1")
                f.truncate()
                # 2) update button (set to unchecked)
                self.enabled = True     
                self.checked = False    

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.