Home >Backend Development >Python Tutorial >Python processing menu message operation example

Python processing menu message operation example

不言
不言Original
2018-05-09 13:42:092012browse

This article mainly introduces the operation of Python to process menu messages, and analyzes the creation of menus and menu item response related operation techniques in Python based on the win32ui module in the form of examples. Friends in need can refer to the examples of this article

Describes how Python handles menu message operations. Share it with everyone for your reference, the details are as follows:

1. Code

# -*- coding:utf-8 -*-
#! python3
import win32ui
import win32api
from win32con import *
from pywin.mfc import window
class MyWnd(window.Wnd):
  def __init__ (self):
    window.Wnd.__init__(self,win32ui.CreateWnd())
    self._obj_.CreateWindowEx(WS_EX_CLIENTEDGE,\
                 win32ui.RegisterWndClass(0,0,COLOR_WINDOW+1),\
                 'www.jb51.net - MFC GUI',WS_OVERLAPPEDWINDOW,\
                 (10,10,800,500),None,0,None)
    submenu = win32ui.CreateMenu()
    menu = win32ui.CreateMenu()
    submenu.AppendMenu(MF_STRING,1051,'&Open')
    submenu.AppendMenu(MF_STRING,1052,'&Close')
    submenu.AppendMenu(MF_STRING,1053,'&Save')
    menu.AppendMenu(MF_STRING|MF_POPUP,submenu.GetHandle(),'&File')
    self._obj_.SetMenu(menu)
    self.HookCommand(self.MenuClick,1051)
    self.HookCommand(self.MenuClick,1052)
    self.HookCommand(self.MenuClick,1053)
  def OnClose(self):
    self.EndModalLoop(0)
  def MenuClick(self,lParam,wParam):
    if lParam == 1051:
      self.MessageBox('Open','Python',MB_OK)
    elif lParam == 1053:
      self.MessageBox('Sava','python',MB_OK)
    else:
      self.OnClose()
w = MyWnd()
w.ShowWindow()
w.UpdateWindow()
w.RunModalLoop(1)

Note:win32ui The module can be installed directly using the pip command, as follows:

pip install pypiwin32

Related recommendations:

Python uses Windows API to create a window example

The above is the detailed content of Python processing menu message operation example. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn