问题
I am a Python noob so I may be missing something here, but I have a problem with how a string is handled inside my program. When I display it, only the first character is displayed.
# some code
MessageBox = ctypes.windll.user32.MessageBoxA
# some other code
testString = self.statusBar1.GetStatusText(0)
# displays "azertyu"
MessageBox(None, "azertyu", 'COUCOU', 0)
# displays 'M'
MessageBox(None, testString, 'COUCOU3', 0)
# displays 'a'
MessageBox(None, testString[1:], 'COUCOU3', 0) #
#displays 'c'
MessageBox(None, testString[2:], 'COUCOU3', 0)
The full string is 'Machine' (it's actually longer than that). How comes Python considers any character is the ending one and displays only one character at once ? Am I missing some Python basics here ?
PS. GetStatusText reference is available at http://www.wxpython.org/docs/api/wx.StatusBar-class.html#GetStatusText. I have tested GetStatusText with a very long string and it doesn't seem to cut texts.
回答1:
If you are using wxPython, why are you trying to show a message box with ctypes? The wxPython package has its own message dialogs. See the following links:
- http://wiki.wxpython.org/MessageBoxes
- http://wxpython.org/docs/api/wx.MessageDialog-class.html
- http://www.blog.pythonlibrary.org/2010/07/10/the-dialogs-of-wxpython-part-2-of-2/
The wxPython demo package (downloadable from the wxPython website) has examples of MessageDialog and GenericMessageDialog.
回答2:
MessageBoxA is the ascii version of the MessageBox win32 API. Your testString is probably a Unicode value, so the value being passed to MessageBoxA will end up looking like an array of bytes with a zero in every other index. In other words it looks like a character string with just one character terminated by a NULL character. I bet if you use str(testString) or switch to MessageBoxW then it will work as expected, however you really should be using wx.MessageBox or wx.MessageDialog instead.
回答3:
It's treating the testString as a list
In [214]: for x in "Machine":
.....: print x
.....:
M
a
c
h
i
n
e
Have you tried ?
MessageBox(None, [testString], 'COUCOU3', 0)
as it's as if MessageBox
is expecting a list of txt, which might makes sense:
["DANGER", "Will Robinson"]
Would then give two lines of txt on your message.
PURE GUESSWORK
来源:https://stackoverflow.com/questions/14648448/string-problems-with-python