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:
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:
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 -*-
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]
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