0

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.

Toto
  • 397
  • 6
  • 17
  • 1
    Looks like a problem with the MessageBox, not how Python handles strings. (What does `logging.debug()` tell you the string is?) – Wooble Feb 01 '13 at 14:29
  • What is the result of `type(testString)`? Maybe you get a list of characters back instead of an actual string object? A fix in that situation would be `testString = "".join(testString)`. – filmor Feb 01 '13 at 14:30
  • I don't think you are calling `MessageBox` correctly. Skimming the documentation the signature is `int MessageBox(message, caption, style, etc...)` So I think you are passing in incorrect parameters. Maybe you are mixing things up with `MessageDialog`? – Bakuriu Feb 01 '13 at 14:44
  • My MessageBox calls comes from that page http://stackoverflow.com/questions/4485610/python-message-box-without-huge-library-dependancy Sorry for being so clueless, but how I am supposed to use logging.debug() or type() ? I'm running my application from a Windows console prompt (T:\toto\app.py) – Toto Feb 01 '13 at 15:36
  • Here is what I get from logging.debug() : DEBUG:root:Machine: Host1 It's exactly what I want ('Machine: Host1'). Now I have to understand why the MessageBox truncates it. It's not the ":" because I have tested a string constant with that character in it, and it displays ok. – Toto Feb 01 '13 at 15:54

4 Answers4

7

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.

RobinDunn
  • 6,116
  • 1
  • 15
  • 14
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:

The wxPython demo package (downloadable from the wxPython website) has examples of MessageDialog and GenericMessageDialog.

Mike Driscoll
  • 32,629
  • 8
  • 45
  • 88
  • Thanks for the suggestion. I have tried wx.MessageBox and it works. It displays the full string. I'm still puzzled why MessageBox combined with a sliced GetStatusText wouldn't display the whole string...but at least I know how to circumvent it. Thank you ! – Toto Feb 05 '13 at 09:44
1

Try ctypes.windll.user32.MessageBoxW instead of ctypes.windll.user32.MessageBoxA:

import ctypes
ctypes.windll.user32.MessageBoxW(None, "Hello, world!", "Test", 0)
Stelios333
  • 498
  • 4
  • 8
0

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

sotapme
  • 4,695
  • 2
  • 19
  • 20
  • This doesn't seem to work. With this the MessageBox statement is skipped entirely (or maybe raise an exception, I haven't checked in details). Thanks anyway for trying to help – Toto Feb 01 '13 at 15:58
  • As I said *pure guesswork* - Have you seen it work by calling ``MessageBox(None, "Hello World!", 'COUCOU3', 0)`` as from Mike Driscoll's' answer it seems obvious that it should work. Having said that I just noticed it's using ``ctypes`` - so I presume it's not ``wx` - this may help http://stackoverflow.com/questions/1005117/ctypes-in-python-2-6-help – sotapme Feb 01 '13 at 18:51