4

I have a simple Python application that lives in system tray in Windows XP. The app is using pywin32 (Build 218) for its GUI and runs on Python 2.7.6. Everything was smooth until I tried to create a menu item with Unicode value:

# -*- coding: utf-8 -*-
import win32gui

...

menu = win32gui.CreatePopupMenu()
win32gui.AppendMenu(menu, win32con.MF_STRING, 1, u'Выход')

This menu item renders as five question marks ('?????') while ascii strings work just fine.

If I change the string literal definition for Python 3 and run it on Python 3.3.3, the menu item text displays correctly:

win32gui.AppendMenu(menu, win32con.MF_STRING, 1, 'Выход')

Renders as 'Выход'.

I have to stick with Python 2 as some of the modules the app is using are not compatible with 3.x.x.

Similar issues (#1, #2) on SO with C WinAPI seem to be resolved by defining UNICODE and using L"" string literals. But I don't know how to accomplish this with pywin32. Using utf-16le encoding for the string doesn't help.

Community
  • 1
  • 1
Warwick
  • 1,200
  • 12
  • 22

1 Answers1

4

ctypes allows to call native Win32 APIs.

Use ctypes.windll.User32.AppendMenuW

manuell
  • 7,528
  • 5
  • 31
  • 58
  • Can you give an example. Above function do not accept structure from "win32gui_struct.PackMENUITEMINFO" with error "ctypes.ArgumentError: argument 4: : Don't know how to convert parameter 4" – Durgaprasad Feb 13 '18 at 12:04
  • @Durgaprasad Soory, I can't understand your question. The parameter 4 is either an unicode string, a bitmap handle, or an arbitrary 32 bits value. It depends on bits in the parameter 2. See https://msdn.microsoft.com/en-us/library/windows/desktop/ms647616 – manuell Feb 13 '18 at 16:35
  • Got the answer. addressof(Hmenu01Info) will be 4th parameter. Another question is what is difference between MENUITEMINFO and MENUITEMINFOW. Second one is recomanded for InsertMenuItemW. – Durgaprasad Feb 14 '18 at 13:58
  • @Durgaprasad See https://stackoverflow.com/questions/7424383/what-is-the-difference-between-the-a-and-w-functions-in-the-win32-api – manuell Feb 14 '18 at 15:43