wxpython using variable with wx.statictext ignores \n

MRAB python at mrabarnett.plus.com
Fri Jun 1 10:37:29 EDT 2012


On 01/06/2012 07:24, Simon Cropper wrote:
> Hi,
>
> I have some wxPython code created with wxGlade that I am customizing.
>
> I have a label created under the def __init__() section of the Frame
> Class. It states...
>
>           self.Question = wx.StaticText(self, -1, "My question...")
>
> if I insert a new line character in this string like this then the
> output is placed on two lines as expected.
>
>           self.Question = wx.StaticText(self, -1, "My \nquestion...")
>
>           Output   My
>                    question
>
> I am getting the question as an argument using sys.argv[] and putting
> it in a variable called "question_text". The code is as follows...
>
>
> 	question_text = "My \nquestion..."
>           self.Question = wx.StaticText(self, -1, question_text)
>
>           Output   My \nquestion
>
> How do I get the line text \n recognized so the break is inserted using
> the variable technique like what happened when I actually used a string?
>
> ...in bash you would do "$question_text". Is there comparable macro
> substitution in python.
>
In a Python script, a \n within a plain string literal such as "My
\nquestion..." will be treated as a newline character.

If you're getting the string from the command line, a \n will be
treated as backslash '\' followed by letter 'n', so it's equivalent to
the string literal "\\n".

In Python 2, you can unescape a string using its .decode method:

 >>> "My \\nquestion..."
'My \\nquestion...'
 >>> "My \\nquestion...".decode("string_escape")
'My \nquestion...'



More information about the Python-list mailing list