3

I have written a simple python program in version 2.7.6 (32 bit). But when i display any message in a message box it comes in some weird language. The code is below

import Tkinter as tk
import win32com.client
import pythoncom
import ctypes
import sys
import glob
import sys
import os

MessageBox = ctypes.windll.user32.MessageBoxW 

if __name__ == "__main__":
   MessageBox(None, "Hello", 'Window title',0)

and this is the output

error output

4
  • Is that your complete program? Commented Dec 26, 2013 at 10:35
  • 1
    this is a sample program i have written just to show my problem Commented Dec 26, 2013 at 10:37
  • i am still not sure if it is the installer problem from where i have downloaded. the url i downloaded was from python.org/download/releases/2.7.6 Commented Dec 26, 2013 at 10:41
  • Its not an installer problem; you need to send a unicode string otherwise your normal string will be interpreted incorrectly. Commented Dec 26, 2013 at 10:46

1 Answer 1

10

You need to send a unicode string; because you are using the unicode version of message box MessageBoxW, if you want to send normal ascii strings you need to use MessgeBoxA

ctypes.windll.user32.MessageBoxA(None, 'Hello', 'Window title', 0) # or
ctypes.windll.user32.MessageBoxW(None, u'Hello', u'Window title', 0)
Sign up to request clarification or add additional context in comments.

4 Comments

that did work. but i never had to do this when i had installed python 3.3. so is this different in 2.7 and 3.3?\
In Python3, all strings are unicode objects that's why it worked.
You're supposed to set the function pointer's argtypes to use c_wchar_p. Then you can call ctypes.set_conversion_mode('utf-8', 'strict'), or whatever encoding you prefer, to configure automatic conversion of byte strings to unicode. set_conversion_mode is only in 2.x ctypes. The default conversion mode is strict ASCII.
what's the "None" stand for?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.