6.2.3 how to add a toolbar
The menu bar and toolbar are usually closely linked, and most of the functions of the toolbar correspond to menu items. In wxpython, the Wx. evt_menu event is triggered when the toolbar is hit, so that the same method can be easily used to process the selection of menu items and the hitting of the toolbar. A wxpython toolbar is an instance of the Wx. toolbar class. As we can see in Chapter 2, you can use the createmedilbar () method of the framework to create a toolbar. Like the status bar, the toolbar size changes automatically with the change of its parent frame. The toolbar can have any sub-windows like other wxpython windows. The toolbar also contains the method for creating tool buttons. Figure 6.4 shows a part of the sketch window with a toolbar that uses the corresponding menu methods in 6.2.2 to correspond to the menu item functions.
Fig 6.4
In Example 6.5, we used a new method instead of repeatedly using the menu to change the paint color.
Example 6.5 Add a toolbar to the sketch application
Def _ init _ (self, parent ):
Wx. Frame. _ init _ (self, parent,-1, "sketch frame ",
Size = (800,600 ))
Self. Sketch = sketchwindow (self,-1)
Self. Sketch. BIND (wx. evt_motion, self. onsketchmotion)
Self. initstatusbar ()
Self. createmenubar ()
Self. createmedilbar ()
Def createmedilbar (Self): #1 create a toolbar
Toolbar = self. createmedilbar ()
For each in self. toolbardata ():
Self. createsimpletool (toolbar, * each)
Toolbar. addseparator ()
For each in self. toolbarcolordata ():
Self. createcolortool (toolbar, each)
Toolbar. Realize () #2 display the toolbar
Def createsimpletool (self, toolbar, label, filename, help, Handler): #3 create a common tool
If not label:
Toolbar. addseparator ()
Return
BMP = wx. Image (filename,
Wx. bitmap_type_bmp). converttobitmap ()
Tool = toolbar. addsimpletool (-1, BMP, label, help)
Self. BIND (wx. evt_menu, handler, tool)
Def toolbardata (Self ):
Return ("new", "new.bmp", "create new sketch ",
Self. onnew ),
("","","",""),
("Open", "open.bmp", "Open existing sketch ",
Self. onopen ),
("Save", "save.bmp", "save existing sketch ",
Self. onsave ))
Def createcolortool (self, toolbar, color): #4 Create a color Tool
BMP = self. makebitmap (color)
Newid = wx. newid ()
Tool = toolbar. addradiotool (-1, BMP, shorthelp = color)
Self. BIND (wx. evt_menu, self. oncolor, tool)
Def makebitmap (self, color): #5 create a solid color bitmap
BMP = wx. emptybitmap (16, 15)
Dc = wx. memorydc ()
DC. SelectObject (BMP)
DC. setbackground (wx. Brush (color ))
DC. Clear ()
DC. SelectObject (wx. nullbitmap)
Return BMP
Def toolbarcolordata (Self ):
Return ("black", "Red", "green", "blue ")
Def oncolor (self, event): #6. Change the paint color to respond to the hitting on the toolbar.
Menubar = self. getmenubar ()
Itemid = event. GETID ()
Item = menubar. finditembyid (Itemid)
If not item:
Toolbar = self. gettoolbar ()
Item = toolbar. findbyid (Itemid)
Color = item. getshorthelp ()
Else:
Color = item. getlabel ()
Self. Sketch. setcolor (color)
#1: The toolbar code is similar to the menu code in settings. However, here we use different loop settings for the regular button and single-choice switch button.
#2: The realize () method is actually a tool bar object layout in the toolbar. It must be called before being displayed on the toolbar. If a tool is added or deleted on the toolbar, this method must also be called.
#3: This method is similar to creating a menu item. The main difference is that the tool on the toolbar requires bitmap display. Here we use three basic bitmaps in the same directory. At the end of the method, we bind the same wx. evt_menu event used by the menu items.
#4: Color tools are similar to conventional tools. The only difference is that a different method is used to tell the toolbar that they are radio tools. A solid color bitmap is created by the makebitmap () method.
#5: This method creates a solid color bitmap for the radio tool.
#6: This method adds a correct search tool to the original to change the color. However, the problem with the code is that the color of the paint brush has changed through the menu item, but the status of the radio tool on the toolbar has not changed, and the opposite is true.
Tools in the toolbar can generate wx. evt_tool_rclicked events when right-clicking. The toolbar also has some different styles that are passed as bitmap parameters to createmedilbar (). Table 6.4 lists some toolbar styles.
Table 6.4 wx. toolbar Style
Wx. tb_3dbuttons: 3D appearance
Wx. tb_horizontal: Default style, toolbar horizontal layout
Wx. tb_noicons: Do not display bitmap for each tool
Wx. tb_text: displays short help text based on different bitmaps.
Wx. tb_vertical: vertical placement Toolbar
The toolbar is more complex than the status bar. Table 6.5 shows some common methods.
Table 6.5 common methods of Wx. Toolbar
Addcontrol: add any wxpython control to the toolbar. The related method is insertcontrol ().
Addseparator (): Place spaces between tools.
Addsimpletool (ID, bitmap, shorthelpstring = "", kind = wx. item_normal): adds a simple tool with a given bitmap to the toolbar. Shorthelpstring is displayed as a prompt. The value of kind can be wx. item_normal, wx. item_checkbox, or WX. item_radio.
Addtool (ID, bitmap, bitmap2 = wx. nullbitmap, kind = wx. item_normal, shorthelpstring = "", longhelpstring = "",
Clientdata = none): Other parameters of the simple tool. Bitmap2 is the bitmap displayed when the tool is pressed. Longhelpstring is the help string displayed in the status bar when the pointer is located in the tool. Clientdata is used to associate any piece of data with a tool. Related Methods: inserttool ().
Addchecktool (...): adds a check box switching tool. The required parameters are the same as addtool ().
Addradiotool (...): adds a single-choice switchover tool. The required parameter is the same as addtool (). Consecutive unseparated radio tools are considered as a group.
Deletetool (toolid)
Deletetoolbyposition (x, y): deletes a tool for a given ID or a tool for a given display position.
Findcontrol (toolid)
Findtoolforposition (x, y): find and return a tool with a given ID or position.
Toggletool (toolid, toggle): sets the status of a tool with a given id based on Boolean toggle.
In the next section, we will show you how to use the general dialog box to obtain user information.