[Python] WxPython menu bar Control Learning Summary (original)

Source: Internet
Author: User

1, Summary 1, the overall creation process

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

      

Equivalent to this white place, no file this menu

2. Create Menu: Filemenu = wx. Menu ()

      

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

3. Create menu item: NewItem = WX. MenuItem ()

For example, New Copy Cut Paste radio_one and so on.

These are called menu items that can be directly "point" (as a button).

You need to assign an ID (int integer) When you create it, and then you need to pass the ID (event) When the menu event is processed. GetId ()) to determine the clicked button.

4. Add menu items to the menus: Filemenu.appenditem (newitem)

This will open the "folder" when we single File.

      

and show the content, add as much as you like and show it in the order added

5. Add the menu to the menu bar: Menubar.append (filemenu, title = "File")

Before we just built a red-framed +file menu with a whole object: Filemenu.

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

6. Set the menu bar to the "menu bar" of our window frame: self. Setmenubar (MenuBar)

The previously created menu bar object is placed in the window.

7. Bind Menu event: Self. Bind (WX. Evt_ment, Self.menuhandler)

Call the bind () function for this binding event, because other components, such as button edit boxes, have events.

So here's wx. Evt_ment is a menu event that represents a binding.

Menuhandler () is a custom function used to menu events.

      

id = event. GetId () is used to determine which menu item is clicked.

Function Parameter name: Event cannot change, only this word.

8, window frame processing: Not related to the menu bar

such as resizing, where it appears, whether it is visible

      

2, the detailed (see other)

1, the use of the class:

Menu bar: WX. MenuBar class

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

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

The number of menus represented by the parameter "n". menu is an array of menus and headings and an array of strings. If the style parameter is set to WX. Mb_dockable, the menu bar can be docked.

      

Menu: WX. Menu class

Wx. The menu class object is a drop-down list that can be selected by a user or items.

The start of the new menu object is as long as Filemenu = WX. Menu () to

After adding a menu item, you can add it directly using the Append () function, or WX. An object of the MenuItem class is appended.

WX. Menu.append (ID, text, kind)  = Wx.menuitem (parentmenu, id, text, kind) Wx. Menu.append (Item)

menu item: WX. MenuItem class

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

NewItem = wx. MenuItem (Parentmenu, id, text, kind)

which

Parentmenu for the opposite of its menu, such as the above mentioned Newmenu menu item parentmenu for Filemenu

ID is the ID of the menu item, and the time to process it will need to be dropped.

Text is the name of this menu item

Kind is the type of the menu item

① How to register shortcut keys: Parameter text = "Name/t shortcut key"

Like text = "Quit/tctrl+q"

This registers the shortcut keys for Ctrl + Q

Ways to ② other types of menu items

There are 3 types of menu items:

      

Only parameter kind = Correlation type constant is required

2. Impressions

During the period looked for quite a lot of information: http://www.yiibai.com/wxpython/wxpython_menus.html#

This is very detailed, and the example is classic.

o.o If you do not understand first, it will be much better to knock on the side.

With regard to the use of the menu bar in Wxpyton, the following code can show the relevant operation methods well.

3. Sample Code

  

1 #Coding:utf-82 #Author:twobox3 4 ImportWX5 6 classMywin (WX. Frame):7     def __init__(self, Parent, title):8Super (Mywin, self).__init__(Parent, title =title)9 Self . Initui ()Ten  One     defInitui (self): A         #Create a menu bar -MenuBar =WX. MenuBar () -  the         #Create a menu 1 -Filemenu =WX. Menu () -  -         #Create a menu item 1-1 +NewItem = wx. MenuItem (filemenu, id = wx.id_new, Text ='New', kind =WX. Item_normal) - Filemenu.appenditem (newitem) +  A         #Add a line at Filemenu.appendseparator () -  -         #Create a submenu 1-2 -Editmenu =WX. Menu () -  -         #Create a menu item for three submenus 1-2-1 and 1-2-2 inCutitem = wx. MenuItem (editmenu, id = 122, Text ="Cut", kind =WX. Item_normal) -CopyItem = wx. MenuItem (editmenu, id = 121, Text ="Copy", kind =WX. Item_normal) toPasteitem = wx. MenuItem (editmenu, id = 123, Text ="Paste", kind =WX. Item_normal) + Editmenu.appenditem (CopyItem) - Editmenu.appenditem (Cutitem) the Editmenu.appenditem (Pasteitem) *  $         #the handle menu 1-2 is added to the menu 1Panax NotoginsengFilemenu.appendmenu (Wx.id_any,"Edit", Editmenu) -  the         #Add a line + Filemenu.appendseparator () A  the         #add two radio boxes 1-3 and 1-4 +Radio1 = wx. MenuItem (filemenu, id = +, Text ="Radio_one", kind =WX. Item_radio) -Radio2 = wx. MenuItem (filemenu, id = +, Text ="Radio_two", kind =WX. Item_radio) $ Filemenu.appenditem (Radio1) $ Filemenu.appenditem (Radio2) -         #PS. A radio box interacts only between its own area (between two lines) -  the         #Add a line - Filemenu.appendseparator ()Wuyi  the         #Add a menu item that can be selected 1-5 -Filemenu.appendcheckitem (id =, item ="Check") Wu  -         #Add a menu item 1-6 and register the shortcut key AboutQuit = wx. MenuItem (filemenu, id = wx.id_exit, Text ="Quit\tctrl+q", kind =WX. Item_normal) $ Filemenu.appenditem (quit) -  -         #Add the Filemenu menu to the menu bar -Menubar.append (Filemenu, title ='File') A  +         #sets the window frame's menu bar to MenuBar the Self . Setmenubar (MenuBar) -  $         #Binding Event Handling the Self . Bind (WX. Evt_menu, Self.menuhandler) the  the         #Let it open the resize display in the middle of the screen theSelf. SetSize ((300,400)) - Self . Centre () in Self . Show () the  the     defMenuhandler (Self, event): AboutID =event. GetId () the         ifid = =wx.id_new: the             Print("NEW") the         ifid = =Wx.id_exit: + exit (0) -  the Bayi  the if __name__=="__main__": theex =WX. APP () -Mywin (None,'menu-test') -     #Mywin (None, ' menu-test ') #可以同时打开两个窗口 really embodies the idea of object-oriented program development theEx. Mainloop ()

2017-08-20 14:21:26

    

[Python] WxPython menu bar Control Learning Summary (original)

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.