[Python] wxPython menu bar Control Learning Summary (original), pythonwxpython

Source: Internet
Author: User

[Python] wxPython menu bar Control Learning Summary (original), pythonwxpython
1. Summary 1. General creation process

1. Create a menu bar: menuBar = wx. MenuBar ()

      

This is equivalent to this white place. There is no File menu.

2. Create a Menu: fileMenu = wx. Menu ()

      

These two are not directly "used", called menus. Folder samples used to classify other menu items

3. Create a menu item: newItem = wx. MenuItem ()

For example, New Copy Cut Paste Radio_One.

The menu items that can be directly "clicked" (as buttons) are called menu items.

Id (int integer) needs to be allocated during creation, and the clicked Button needs to be determined by the passed id (event. GetId () during menu event processing.

4. Add the menu item to the menu: fileMenu. AppendItem (newItem)

In this way, the "folder" will be opened when the single-host File is used"

      

The content is displayed in the order of addition.

5. Add the menu to the menu bar: menuBar. Append (fileMenu, title = "File ")

Previously, we only constructed a whole object of the red box + File menu: fileMenu.

Then we need to add this menu object to the menu bar.

6. Set the menu bar as the "menu bar" of the window framework: self. SetMenuBar (menuBar)

Place the previously created menu bar objects in the window.

7. Bind menu events: self. Bind (wx. EVT_MENT, self. menuHandler)

Call the Bind () function to Bind the event, because other components such as the button editing box have events.

Therefore, wx. EVT_MENT indicates that a menu event is bound.

MenuHandler () is a custom function used for menu events.

      

Id = event. GetId () is used to determine the menu item to be clicked.

Function parameter name: the event cannot be changed. It can only be the word.

8. Processing of the window framework: it has nothing to do with the menu bar

Such as adjusting the size, position, and visibility

      

2. Details (refer to others)

1. Used class:

Menu Bar: wx. MenuBar class

Wx. MenuBar ()
Wx. MenuBar (n, menus, titles, style)

There is a constructor for the parameter, and there is also a default.

"N" indicates the number of menus. Menu is an array of menus and titles and a string array. If the style parameter is set to wx. MB_DOCKABLE, the menu bar can be docked.

      

Menu: wx. Menu class

The wx. Menu class object is one or more Menu items, one of which can be selected from the drop-down list.

When starting the new Menu object, you only need fileMenu = wx. Menu ().

You can use the Append () function to add a menu item or Append an object of the wx. MenuItem class.

      wx.Menu.Append(id, text, kind)              Item = Wx.MenuItem(parentmenu, id, text, kind)       wx.Menu.Append(Item)

 

Menu item: wx. MenuItem class

When we use the new menu item object, we need to fill in several parameters.

newItem = wx.MenuItem(parentMenu, id, text, kind)

 

Where:

ParentMenu is opposite to its menu. For example, the parentMenu of the newMenu menu item mentioned above is fileMenu.

Id is the id of the menu item. when processing the menu item time, you need to use the drop

Text indicates the name of this menu item.

Kind is the type of this menu item

① How to register a shortcut key: parameter text = "name/t shortcut key"

For example, text = "Quit/tCtrl + Q"

In this way, the Ctrl + Q shortcut is registered.

② Methods for menu items of other types

There are three menu items:

      

The parameter kind = constant of the relevant type.

2. Feelings

I saw a lot of information during: http://www.yiibai.com/wxpython/wxpython_menus.html #

This is detailed and the example is classic.

O. o. If you don't understand it first, it would be much better to copy it.

The following code demonstrates how to use the menu bar in wxPyton.

3. Sample Code

  

1 # coding: UTF-8 2 # author: Twobox 3 4 import wx 5 6 class Mywin (wx. frame): 7 def _ init _ (self, parent, title): 8 super (Mywin, self ). _ init _ (parent, title = title) 9 self. initUI () 10 11 def InitUI (self): 12 # create a menu bar 13 menuBar = wx. menuBar () 14 15 # create a menu 116 fileMenu = wx. menu () 17 18 # create a Menu item 1-119 newItem = wx. menuItem (fileMenu, id = wx. ID_NEW, text = 'new', kind = wx. ITEM_NORMAL) 20 fileMenu. appendItem (newItem) 21 22 # add a line 23 fileMenu. appendSeparator () 24 25 # create a sub-menu, 1-226 editMenu = wx. menu () 27 28 # create Menu items 1-2-1 and 1-2-2 and 1-2-2-329 cutItem = wx. menuItem (editMenu, id = 122, text = "Cut", kind = wx. ITEM_NORMAL) 30 copyItem = wx. menuItem (editMenu, id = 121, text = "Copy", kind = wx. ITEM_NORMAL) 31 pasteItem = wx. menuItem (editMenu, id = 123, text = "Paste", kind = wx. ITEM_NORMAL) 32 editMenu. appendItem (copyItem) 33 editMenu. appendItem (cutItem) 34 editMenu. appendItem (pasteItem) 35 36 # Add sub-menu 1-2 to menu 1 37 fileMenu. appendMenu (wx. ID_ANY, "Edit", editMenu) 38 39 # add a line 40 fileMenu. appendSeparator () 41 42 # Add two single partitions 1-3 and 1-443 radio1 = wx. menuItem (fileMenu, id = 13, text = "Radio_One", kind = wx. ITEM_RADIO) 44 radio2 = wx. menuItem (fileMenu, id = 14, text = "Radio_Two", kind = wx. ITEM_RADIO) 45 fileMenu. appendItem (radio1) 46 fileMenu. appendItem (radio2) 47 # PS. A single queue only interacts with each other in its own region (between two lines) 48 49 # add a line 50 fileMenu. appendSeparator () 51 52 # Add an optional menu item 1-553 fileMenu. appendCheckItem (id = 15, item = "Check") 54 55 # Add a menu item 1-6 and register the shortcut key 56 quit = wx. menuItem (fileMenu, id = wx. ID_EXIT, text = "Quit \ tCtrl + Q", kind = wx. ITEM_NORMAL) 57 fileMenu. appendItem (quit) 58 59 # Add the fileMenu to the menu bar 60 menuBar. append (fileMenu, title = 'file') 61 62 # Set the menu bar of the window framework to menuBar63 self. setMenuBar (menuBar) 64 65 # bind event processing 66 self. bind (wx. EVT_MENU, self. menuHandler) 67 68 # Let it open in the middle of the screen to adjust the size of the display 69 self. setSize (300,400) 70 self. centre () 71 self. show () 72 73 def menuHandler (self, event): 74 id = event. getId () 75 if id = wx. ID_NEW: 76 print ("NEW") 77 if id = wx. ID_EXIT: 78 exit (0) 79 80 81 82 if _ name _ = "_ main _": 83 ex = wx. app () 84 Mywin (None, 'menu-test') 85 # Mywin (None, 'menu-test ') # Two windows can be opened at the same time to reflect the object-oriented programming ideology 86 ex. mainLoop ()

14:21:26

 

    

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.