Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I want to get combobox attribute 'dem' and use it for further processing in class ExtentData. But I am not sure how to get attributes from one class to another for processing in pythonaddin. In following code, I want attribute 'dem' from class ComboBoxFrame right after if statement in class ExtentData. Any suggestion would be appreciative.

import arcpy
import pythonaddins
import wx

class ExtentData(object):
"""Implementation for ExtentData_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
        self.cursor = 5
        self.shape = 'Rectangle'

    def onRectangle(self, rectangle_geometry):
    """"""

    dlg = None
    extent = rectangle_geometry

    if extent != None:
        self.dlg = ComboBoxFrame()


class ComboBoxFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Input Data Selection', size=(350, 300))
        self.Bind(wx.EVT_CLOSE, self.OnClose)
        panel = wx.Panel(self, -1)

        DemList = ['DEM1', 'DEM2', 'DEM3']
        self.Dem = wx.ComboBox(panel, -1, "", (15, 30), wx.DefaultSize,DemList, wx.CB_DROPDOWN)
        self.Bind(wx.EVT_COMBOBOX, self.OnSelect, self.Dem)

        self.btnApply = wx.Button(panel, label="Apply", pos=(65, 200))
        self.Bind(wx.EVT_BUTTON, self.OnClose, id=self.btnApply.GetId())
        self.Show(True)

    def OnClose(self, event):
        self.Show(False)

    def OnSelect(self, event):
        dem = str(event.GetValue())

app = wx.PySimpleApp()
app.MainLoop()
share|improve this question
    
Your code imports wx but does not specifically mention wxPython. I would probably create and use a tag for wxpython to try and target those with that expertise. –  PolyGeo Apr 26 '13 at 10:06
    
I was able to solve this issue with the inclusion of line 'global extent' right above 'extent = rectangle_geometry'. This way, attributes were available for all other classes in the script. –  Ibe Apr 26 '13 at 14:50
1  
Excellent - be sure to copy your comment in as an Answer that you can then Accept. –  PolyGeo Apr 26 '13 at 20:03
add comment

2 Answers

The pythonic way to do this, without using global variables, is to define an attribute in either the class's __init__ method (making them instance members), or in the class declaration itself (making them class members).

For example:

class A():
    def __init__(self):
        self.extent = (0, 0, 0, 0)

class B():
    extent = (1, 2, 3, 4)

if __name__ == "__main__":
    a = A()
    print a.extent # prints (0, 0, 0, 0)
    print B.extent # prints (1, 2, 3, 4)
    print A.extent # results in AttributeError: class A has no attribute 'extent'
share|improve this answer
    
This is neat. In my case, class A is calling class B (class B is wx.Frame). I didn't stop 'self.dlg' and it is in some sort of continuing loop. Please suggest how to kill this event once I accomplish something in class B. –  Ibe Apr 27 '13 at 5:43
    
That sounds like a wxPython question which I can't help you with. I suggest you ask a new question. Probably on StackOverflow. –  blah238 Apr 27 '13 at 7:02
add comment
up vote 0 down vote accepted

I was able to solve this issue with the inclusion of line 'global extent' right above 'extent = rectangle_geometry'. This way, attributes were available for all other classes in the script.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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