Python Forum
Get variable from another class - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Get variable from another class (/thread-17312.html)



Get variable from another class - Golfball2k6 - Apr-06-2019

Very new to Python and programming in general. I've watched videos and read tutorials about class variables, but I can't get it through my head how to make "selection" from FileType available in coordpick:

Would appreciate some help!

import arcpy
import pythonaddins

class FileType(object):
    """Implementation for pasdamappa_addin.filetype (ComboBox)"""
    def __init__(self):
        self.items = [".tif", ".jp2", ".sid"]
        self.editable = False
        self.enabled = True

    def onSelChange(self, selection):
        coordpick.enabled = True
        print selection

class coordpick(object):
    """Implementation for pasdamappa_addin.coordpick (Tool)"""
    def __init__(self):
        self.enabled = False
        self.shape = 'NONE'  # Can set to "Line", "Circle" or "Rectangle" for interactive shape drawing and to activate the onLine/Polygon/Circle event sinks.

    def onMouseDownMap(self, x, y, button, shift):
        print str(x)[:3] + str(y + 10000)[:2] + selection



RE: Get variable from another class - ichabod801 - Apr-06-2019

First of all, selection isn't in FileType. You don't save it as an instance attribute in onSelChange. You would probably want to initialize it to a null value as well:

class FileType(object):
    """Implementation for pasdamappa_addin.filetype (ComboBox)"""
    def __init__(self):
        self.items = [".tif", ".jp2", ".sid"]
        self.editable = False
        self.enabled = True
        self.selection = None   # initialize to None
 
    def onSelChange(self, selection):
        coordpick.enabled = True
        self.selection = selection   # save the value as an instance attribute.
Once you have an instance, you can get the attribute with the dot operator:

>>> ft = FileType()
>>> ft.onSelChange('spam')
>>> ft.selection
'spam'