Hello, list!
I have a problem with instantiating a frame that is designed by
XRCED's auto-generated code.
The main problem is that I do not have any problems when I create an
instance of xrcNetDissolveFrame in the following code. But, I
encounter problems when I create the same instance as an add-on dialog
that is invoked from a toolbar. What is strange is I have multiple
frames that have the same structure. I didn't have any problems with
those frames, but somehow I am getting errors with this new frame.
Specific error message I got is the following:
AttributeError: 'NoneType' object has no attribute 'Enable'
Please let me know anything that may resolve this problem.
The relevant code is included in the following:
-- View code that is auto-generated from XRCED
# This file was automatically generated by pywxrc.
# -*- coding: UTF-8 -*-
import wx
import wx.xrc as xrc
__res = None
def get_resources():
""" This function provides access to the XML resources in this
module."""
global __res
if __res == None:
__init_resources()
return __res
class xrcNetDissolveFrame(wx.Frame):
#!XRCED:begin-block:xrcNetDissolveFrame.PreCreate
def PreCreate(self, pre):
""" This function is called during the class's initialization.
Override it for custom setup before the window is created
usually to
set additional window styles using SetWindowStyle() and
SetExtraStyle().
"""
pass
#!XRCED:end-block:xrcNetDissolveFrame.PreCreate
def __init__(self, parent):
# Two stage creation (see http://wiki.wxpython.org/index.cgi/TwoStageCreation)
pre = wx.PreFrame()
self.PreCreate(pre)
get_resources().LoadOnFrame(pre, parent, "NetDissolveFrame")
self.PostCreate(pre)
# Define variables for the controls, bind event handlers
self.MainPanel = xrc.XRCCTRL(self, "MainPanel")
self.NetFilenameField = xrc.XRCCTRL(self, "NetFilenameField")
self.NetFileButton = xrc.XRCCTRL(self, "NetFileButton")
self.AttributePanel = xrc.XRCCTRL(self, "AttributePanel")
self.IDChoice = xrc.XRCCTRL(self, "IDChoice")
self.outputPanel = xrc.XRCCTRL(self, "outputPanel")
self.OutNetFilenameField = xrc.XRCCTRL(self,
"OutNetFilenameField")
self.OutNetFileButton = xrc.XRCCTRL(self, "OutNetFileButton")
self.buttonPanel = xrc.XRCCTRL(self, "buttonPanel")
self.ButtonCompute = xrc.XRCCTRL(self, "ButtonCompute")
self.ButtonSplit = xrc.XRCCTRL(self, "ButtonSplit")
self.ButtonClose = xrc.XRCCTRL(self, "ButtonClose")
self.Bind(wx.EVT_BUTTON, self.OnButton_NetFileButton,
self.NetFileButton)
self.Bind(wx.EVT_CHOICE, self.OnChoice_IDChoice,
self.IDChoice)
self.Bind(wx.EVT_BUTTON, self.OnButton_OutNetFileButton,
self.OutNetFileButton)
self.Bind(wx.EVT_BUTTON, self.OnButton_ButtonCompute,
self.ButtonCompute)
self.Bind(wx.EVT_BUTTON, self.OnButton_ButtonSplit,
self.ButtonSplit)
self.Bind(wx.EVT_BUTTON, self.OnButton_ButtonClose,
self.ButtonClose)
self.Bind(wx.EVT_CLOSE, self.OnClose)
#!XRCED:begin-block:xrcNetDissolveFrame.OnButton_NetFileButton
def OnButton_NetFileButton(self, evt):
# Replace with event handler code
print "OnButton_NetFileButton()"
#!XRCED:end-block:xrcNetDissolveFrame.OnButton_NetFileButton
#!XRCED:begin-block:xrcNetDissolveFrame.OnChoice_IDChoice
def OnChoice_IDChoice(self, evt):
# Replace with event handler code
print "OnChoice_IDChoice()"
#!XRCED:end-block:xrcNetDissolveFrame.OnChoice_IDChoice
#!XRCED:begin-block:xrcNetDissolveFrame.OnButton_OutNetFileButton
def OnButton_OutNetFileButton(self, evt):
# Replace with event handler code
print "OnButton_OutNetFileButton()"
#!XRCED:end-
block:xrcNetDissolveFrame.OnButton_OutNetFileButton
#!XRCED:begin-block:xrcNetDissolveFrame.OnButton_ButtonCompute
def OnButton_ButtonCompute(self, evt):
# Replace with event handler code
print "OnButton_ButtonCompute()"
#!XRCED:end-block:xrcNetDissolveFrame.OnButton_ButtonCompute
#!XRCED:begin-block:xrcNetDissolveFrame.OnButton_ButtonSplit
def OnButton_ButtonSplit(self, evt):
# Replace with event handler code
print "OnButton_ButtonSplit()"
#!XRCED:end-block:xrcNetDissolveFrame.OnButton_ButtonSplit
#!XRCED:begin-block:xrcNetDissolveFrame.OnButton_ButtonClose
def OnButton_ButtonClose(self, evt):
# Replace with event handler code
print "OnButton_ButtonClose()"
#!XRCED:end-block:xrcNetDissolveFrame.OnButton_ButtonClose
#!XRCED:begin-block:xrcNetDissolveFrame.OnClose
def OnClose(self, evt):
# Replace with event handler code
print "OnClose()"
#!XRCED:end-block:xrcNetDissolveFrame.OnClose
# ------------------------ Resource data ----------------------
def __init_resources():
global __res
__res = xrc.EmptyXmlResource()
wx.FileSystem.AddHandler(wx.MemoryFSHandler())
view_xrc = '''\
<?xml version="1.0" ?><resource class="wxPanel">
<object class="wxFrame" name="NetDissolveFrame">
<object class="wxBoxSizer">
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<object class="wxPanel" name="MainPanel">
<object class="wxBoxSizer">
<orient>wxVERTICAL</orient>
<object class="sizeritem">
<object class="wxStaticBoxSizer">
<object class="sizeritem">
<object class="wxBoxSizer">
<object class="sizeritem">
<object class="wxStaticText"
name="NetFileLabel">
<label>Network Shapefile:</label>
</object>
<option>0</option>
</object>
<object class="sizeritem">
<object class="wxTextCtrl"
name="NetFilenameField">
<size>160,-1</size>
<style>wxTE_READONLY</style>
<XRCED>
<assign_var>1</assign_var>
</XRCED>
</object>
<option>0</option>
</object>
<object class="sizeritem">
<object class="wxBitmapButton"
name="NetFileButton">
<size>22, 22</size>
<bitmap stock_id="wxART_FILE_OPEN"/>
<XRCED>
<events>EVT_BUTTON</events>
<assign_var>1</assign_var>
</XRCED>
</object>
<flag>wxLEFT|wxRIGHT</flag>
<border>2</border>
</object>
<orient>wxHORIZONTAL</orient>
</object>
<flag>wxBOTTOM|wxEXPAND</flag>
<border>5</border>
</object>
<label>Input</label>
<orient>wxVERTICAL</orient>
</object>
<flag>wxALL|wxEXPAND</flag>
<border>5</border>
</object>
<object class="sizeritem">
<object class="wxStaticBoxSizer">
<object class="sizeritem">
<object class="wxPanel" name="AttributePanel">
<object class="wxBoxSizer">
<object class="sizeritem">
<object class="wxStaticText"
name="AttributeLabel">
<label>Attribute Column:</label>
</object>
<option>0</option>
<flag>wxALIGN_CENTRE</flag>
</object>
<object class="sizeritem">
<object class="wxChoice" name="IDChoice">
<content/>
<size>160,-1</size>
<XRCED>
<events>EVT_CHOICE</events>
<assign_var>1</assign_var>
</XRCED>
</object>
<option>0</option>
<flag>wxALIGN_CENTRE</flag>
</object>
<object class="spacer">
<size>22, 22</size>
</object>
<orient>wxHORIZONTAL</orient>
</object>
<XRCED>
<assign_var>1</assign_var>
</XRCED>
</object>
<flag>wxALL|wxEXPAND</flag>
</object>
<label>Parameter</label>
<orient>wxVERTICAL</orient>
</object>
<flag>wxALL|wxEXPAND</flag>
<border>5</border>
</object>
<object class="sizeritem">
<object class="wxStaticBoxSizer">
<object class="sizeritem">
<object class="wxPanel" name="outputPanel">
<object class="wxBoxSizer">
<object class="sizeritem">
<object class="wxStaticText"
name="OutNetFileLabel">
<label>Output Shapefile:</label>
</object>
</object>
<orient>wxHORIZONTAL</orient>
<object class="sizeritem">
<object class="wxTextCtrl"
name="OutNetFilenameField">
<size>160,-1</size>
<style>wxTE_READONLY</style>
<XRCED>
<assign_var>1</assign_var>
</XRCED>
</object>
</object>
<object class="sizeritem">
<object class="wxBitmapButton"
name="OutNetFileButton">
<size>22, 22</size>
<bitmap stock_id="wxART_FILE_OPEN"/>
<XRCED>
<events>EVT_BUTTON</events>
<assign_var>1</assign_var>
</XRCED>
</object>
</object>
</object>
<XRCED>
<assign_var>1</assign_var>
</XRCED>
</object>
<flag>wxALL|wxEXPAND</flag>
</object>
<label>Output</label>
<orient>wxVERTICAL</orient>
</object>
<flag>wxALL|wxEXPAND</flag>
<border>5</border>
</object>
<object class="sizeritem">
<object class="wxStaticBoxSizer">
<object class="sizeritem">
<object class="wxPanel" name="buttonPanel">
<object class="wxBoxSizer">
<object class="sizeritem">
<object class="wxButton" name="ButtonCompute">
<size>80, 20</size>
<label>Compute</label>
<XRCED>
<events>EVT_BUTTON</events>
<assign_var>1</assign_var>
</XRCED>
</object>
</object>
<object class="spacer">
<size>22,22</size>
</object>
<object class="sizeritem">
<object class="wxButton" name="ButtonSplit">
<size>80, 20</size>
<label>Split</label>
<XRCED>
<events>EVT_BUTTON</events>
<assign_var>1</assign_var>
</XRCED>
<enabled>0</enabled>
</object>
</object>
<object class="spacer">
<size>22,22</size>
</object>
<object class="sizeritem">
<object class="wxButton" name="ButtonClose">
<size>80, 20</size>
<label>Close</label>
<XRCED>
<events>EVT_BUTTON</events>
<assign_var>1</assign_var>
</XRCED>
</object>
<flag>wxALIGN_RIGHT</flag>
</object>
<orient>wxHORIZONTAL</orient>
</object>
<XRCED>
<assign_var>1</assign_var>
</XRCED>
</object>
<flag>wxALL|wxEXPAND</flag>
</object>
<label>Actions</label>
<orient>wxHORIZONTAL</orient>
</object>
<flag>wxALL|wxEXPAND|wxALIGN_CENTRE</flag>
<border>5</border>
</object>
</object>
<XRCED>
<assign_var>1</assign_var>
</XRCED>
</object>
<option>1</option>
<flag>wxEXPAND</flag>
</object>
</object>
<pos>70, 70</pos>
<title>Dissolve Network</title>
<font>
<sysfont>wxSYS_DEFAULT_GUI_FONT</sysfont>
</font>
<style>wxCLOSE_BOX|wxMINIMIZE_BOX</style>
<XRCED>
<events>EVT_CLOSE</events>
</XRCED>
</object>
</resource>'''
wx.MemoryFSHandler.AddFile('XRC/view/view_xrc', view_xrc)
__res.Load('memory:XRC/view/view_xrc')
# ----------------------- Gettext strings ---------------------
def __gettext_strings():
# This is a dummy function that lists all the strings that are
used in
# the XRC file in the _("a string") format to be recognized by GNU
# gettext utilities (specificaly the xgettext utility) and the
# mki18n.py script. For more information see:
# http://wiki.wxpython.org/index.cgi/Internationalization
def _(str): pass
_("Network Shapefile:")
_("Input")
_("Attribute Column:")
_("Parameter")
_("Output Shapefile:")
_("Output")
_("Compute")
_("Split")
_("Close")
_("Actions")
_("Dissolve Network")
- A subclass of xrcNetDissolveFrame
import os.path
import wx
from wxpysal import remapEvtsToDispatcher
from view import xrcNetDissolveFrame
from model import DissolveModel
from wxpysal.geodanet.model import Model as geodanet
DEBUG = True
BAD_NETWORK_FILE_LONG = "The supplied shapefile is of type: %s\nYour
network shapefile must contain lines."
BAD_NETWORK_FILE_SHORT = "Bad Network File!"
class DissolveFrame(xrcNetDissolveFrame):
def __init__(self, parent = None):
remapEvtsToDispatcher(self,self.evtDispatch)
xrcNetDissolveFrame.__init__(self,parent)
print type(self.ButtonCompute)
self.dispatch = d = {}
d['NetFileButton'] = self.networkFile # one to respond to a
call from view
d['networkFilename'] = self.networkFile # onf to respond to a
call from model
d['IDChoice'] = self.attribute
d['networkAttributeField'] = self.attribute
d['OutNetFileButton'] = self.netOutput
d['netOutput'] = self.netOutput
d['ButtonCompute'] = self.compute
d['ButtonSplit'] = self.split
d['ButtonClose'] = self.OnClose
d['computationStatus'] = self.computationStatus
d['computationMessage'] = self.computationStatus
self.progressDlg = wx.ProgressDialog("Computation
Status","Message...", maximum = 100, parent = self,
style=wx.PD_APP_MODAL|wx.PD_AUTO_HIDE|wx.PD_CAN_ABORT)
self.progressDlg.SetSize((500,-1))
self.progressDlg.Hide()
self.model = DissolveModel()
self.model.addListener(self.update)
self.model.update()
def evtDispatch(self,evtName, evt):
evtName,widgetName = evtName.rsplit('_',1)
if widgetName in self.dispatch:
self.dispatch[widgetName](evtName,evt)
else:
if DEBUG: print "not implemented:", evtName, widgetName
def update(self,tag=False):
if tag:
if tag in self.dispatch:
self.dispatch[tag](value=self.model.getByTag(tag))
else:
if DEBUG: print "Warning: %s, has not been
implemented"%tag
else:
for key,value in self.model:
if key in self.dispatch:
self.dispatch[key](value=value)
else:
if DEBUG: print "Warning: %s, has not been
implemented"%key
self.able()
def computationStatus(self, evtName=None, evt=None, value=None):
if type(value) == int and value >= 0 and value < 100:
if not self.progressDlg.IsShown() and
self.model.computationStatus:
self.progressDlg.Show()
self.MainPanel.Disable()
c,s = self.progressDlg.Update(value,
self.model.computationMessage)
if not c:
result = wx.MessageDialog(self,"Are you sure you want
to cancel? All progress will be lost!","CANCEL",wx.ICON_QUESTION|
wx.YES_NO).ShowModal()
if result == wx.ID_YES:
self.model.computationContinue = False
self.progressDlg.Resume()
if self.progressDlg.IsShown() and self.model.computationStatus
== 0:
self.progressDlg.Hide()
self.MainPanel.Enable()
def able(self):
if self.model.isvalid:
self.ButtonCompute.Enable(True)
else:
self.ButtonCompute.Enable(False)
if not self.model.networkFilename:
self.AttributePanel.Enable(False)
self.outputPanel.Enable(False)
self.buttonPanel.Enable(False)
else:
self.AttributePanel.Enable(True)
self.outputPanel.Enable(True)
self.buttonPanel.Enable(True)
def networkFile(self, evtName=None, evt=None, value=None):
if evt and evtName=='OnButton':
dlg = wx.FileDialog(self, "Open Network Shapefile",
wildcard='Shapefile (.shp)|*.shp')
if dlg.ShowModal() == wx.ID_OK:
pth = dlg.GetPath()
typ = geodanet.geo_file_type(pth)
if typ != 'ARC':
wx.MessageDialog(self,BAD_NETWORK_FILE_LONG%
(typ),BAD_NETWORK_FILE_SHORT,style=wx.OK|wx.ICON_ERROR).ShowModal()
else:
self.model.networkFilename = pth
elif value != None and value !=
self.NetFilenameField.GetValue():
# update the Network Path display.
self.NetFilenameField.SetValue(self.model.networkFilename)
self.NetFilenameField.SetInsertionPointEnd()
# Update the list of Fields to select Edge Length from.
self.IDChoice.Clear()
self.IDChoice.Append('None')
self.IDChoice.AppendItems(geodanet.file_data_fields(self.model.networkFilename))
self.model.networkAttrField = 'None'
def attribute(self, evtName=None, evt=None, value=None):
if evt and evtName == 'OnChoice':
self.model.networkAttrField =
self.IDChoice.GetStringSelection()
elif value != None:
if self.IDChoice.GetStringSelection() != value:
self.IDChoice.SetStringSelection(value)
def netOutput(self, evtName=None, evt=None, value=None):
if evt and evtName == 'OnButton':
filename = 'dissolved_network.shp'
dlg = wx.FileDialog(self, message="Save Output File As",
style=wx.SAVE|wx.OVERWRITE_PROMPT, defaultFile=filename,
wildcard='Shape files (.shp)|*.shp')
if dlg.ShowModal() == wx.ID_OK:
pth = dlg.GetPath()
self.model.netOutput = pth
elif value != None:
self.OutNetFilenameField.SetValue(value)
self.OutNetFilenameField.SetInsertionPointEnd()
def split(self, evtName=None, evt=None, value=None):
pass
def compute(self, evtName=None, evt=None, value=None):
self.model.update()
self.model.verbose()
print "RUN"
self.model.compute()
print "END_RUN"
def OnClose(self, evtName=None, evt=None, value=None):
self.Hide()
--An excerpt from toolbar's controller code
class ToolbarFrame(view.xrcGeoDaNetMainFrame):
def __init__(self, app, parent = None):
self.app = app
remapEvtsToDispatcher(self,self.evtDispatch)
view.xrcGeoDaNetMainFrame.__init__(self,parent)
self.dispatch = d = {}
d['Quit'] = self.close
d['QuitMenuItem'] = self.close
#d['DissolveTool'] = self.show_dissolve_tool
#d['SplitTool'] = self.show_split_tool
d['DistsTool'] = self.show_dists_tool
d['DistsMenuItem'] = self.show_dists_tool
d['PreferencesMenuItem'] = self.show_pref_tool
d['KFuncsTool'] = self.show_kfuncs_tool
d['KFuncsMenuItem'] = self.show_kfuncs_tool
d['AccessTool'] = self.show_access_tool
d['AccessMenuItem'] = self.show_access_tool
d['KernelTool'] = self.show_kernel_tool
d['KernelMenuItem'] = self.show_kernel_tool
d['KClusterTool'] = self.show_k_cluster_tool
d['KClusterMenuItem'] = self.show_k_cluster_tool
d['LINCsTool'] = self.show_lincs_tool
d['LINCsMenuItem'] = self.show_lincs_tool
d['KFuncVisMenuItem'] = self.show_kfunc_vis_tool
d['DensityVisMenuItem'] = self.show_density_vis_tool
d['PlanarDensityVisMenuItem'] =
self.show_planar_density_vis_tool
d['KClusterVisMenuItem'] = self.show_klincs_vis_tool
d['LincsVisMenuItem'] = self.show_lincs_vis_tool
#self.split_tool = split_controller.SplitFrame(self)
self.dists_tool = dists_controller.DistsController()
self.kfuncs_tool = kfunction_controller.KFunctionFrame(self)
self.kernel_tool = kernel_controller.KernelDensityFrame(self)
self.dissolve_tool = dissolve_controller.DissolveFrame(self)
self.access_tool = access_controller.AccessController()
self.k_cluster_tool =
k_cluster_controller.KClusterController()
self.lincs_tool = lincs_controller.LINCsController()
self.kfunc_vis_tool =
kfunc_vis_controller.KFuncVisController()
self.density_vis_tool =
density_vis_controller.DensityVisController()
self.planar_density_vis_tool =
planar_density_vis_controller.PlanarDensityVisController()
self.klincs_vis_tool =
klincs_vis_controller.KlincsVisController()
self.lincs_vis_tool =
lincs_vis_controller.LincsVisController()
self.pref_tool =
preferences_controller.PreferencesController()
wx.ToolTip.SetDelay(10) # not supported by Mac.