1

I've been trying to create a MessageBox with accented characters. To do so, I used the decode() and encode() functions. The code is part of a Python Toolbox.

pythonaddins.MessageBox("ééàà".decode('utf-8').encode('cp1252'), "My title", 0)

And that's the result I get:

MessageBox with wrong characters

I've also set the default encoding to utf-8 at the beginning of my script:

`# -- coding: utf-8 --

Strangely, when I try it directly in the ArcGIS Python console, it gives the right result:

Good result

It seems like running the code from the Python console in ArcGIS doesn't give the same result as running it from a Python tool ...

I've tried lots of things to fix this like:

  • Setting the text string in Unicode

pythonaddins.MessageBox(u'ééàà'.encode('cp1252'), "My title", 0)

  • Using Windows popups

ctypes.windll.user32.MessageBoxA(0, "ééàà", "My title", 0)

... and it still behaves the same way (works in ArcGIS Python console but not from the toolbox).

Ideas why this is happening?


EDIT: This is an example of tool code that doesn't give the right result:

# -*- coding: utf-8 -*- import arcpy import pythonaddins

class MsgBoxTest(object):

def __init__(self):
    """Define the toolbox (the name of the toolbox is the name of the
    .pyt file)."""
    self.label = "MsgBoxTest"
    self.alias = ""

    # List of tool classes associated with this toolbox
    self.tools = [MsgBoxTest]

class MsgBoxTest(object):

def __init__(self):
    """Define the tool (tool name is the name of the class)."""
    self.label = "MsgBoxTest"
    self.description = ""
    self.canRunInBackground = False

def getParameterInfo(self):
    """Define parameter definitions"""
    params = None
    return params

def isLicensed(self):
    """Set whether tool is licensed to execute."""
    return True

def updateParameters(self, parameters):
    """Modify the values and properties of parameters before internal
    validation is performed.  This method is called whenever a parameter
    has been changed."""
    return

def updateMessages(self, parameters):
    """Modify the messages created by internal validation for each tool
    parameter.  This method is called after internal validation."""
    return

def execute(self, parameters, messages):
    """The source code of the tool."""
    message = "ààéé"
    pythonaddins.MessageBox(message.decode('utf-8').encode('cp1252'),"Attention",0)
    return
  • I think we need you to provide a slightly longer code snippet that works up to where you are stuck. I am not sure that mixing Python AddIn functionality with Python Toolbox code is appropriate because the former accesses the ArcMap GUI. – PolyGeo Jun 15 '17 at 20:28
  • @PolyGeo The rest of my code isn't really linked with the message box, I don't think it could help. I can tell you that there is a return statement just after the message box line. Also, I can tell you that this code line is located in the Python toolbox. I think that your idea is quite on point however. I'm gonna do some research on this. – mattcp2 Jun 16 '17 at 16:32
  • I wasn't thinking about the rest of your code, just a test Python Toolbox with one test tool that potential answerers could copy/paste and open to see what you are describing. – PolyGeo Jun 16 '17 at 19:33
  • I think that this might be a bug with pythonaddins.MessageBox, I'm having the same problem and am looking for a workaround. – user2345451 Jun 30 '17 at 16:05
  • Yes, I do think it's a bug coming from pythonaddins too! I think the next step would be warning ESRI about this. – mattcp2 Jul 3 '17 at 12:19

Your Answer

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

Browse other questions tagged or ask your own question.