]>
git.saurik.com Git - wxWidgets.git/blob - wxPython/demo/ToolBar.py
   5 #--------------------------------------------------------------------------- 
   7 class TestToolBar(wx
.Frame
): 
   8     def __init__(self
, parent
, log
): 
   9         wx
.Frame
.__init
__(self
, parent
, -1, 'Test ToolBar', size
=(500, 300)) 
  12         self
.Bind(wx
.EVT_CLOSE
, self
.OnCloseWindow
) 
  14         wx
.Window(self
, -1).SetBackgroundColour(wx
.NamedColour("WHITE")) 
  16         # Use the wxFrame internals to create the toolbar and associate it all 
  17         # in one tidy method call. 
  18         tb 
= self
.CreateToolBar( wx
.TB_HORIZONTAL
 
  24         # Here's a 'simple' toolbar example, and how to bind it using SetToolBar() 
  25         #tb = wx.ToolBarSimple(self, -1, wx.DefaultPosition, wx.DefaultSize, 
  26         #               wx.TB_HORIZONTAL | wx.NO_BORDER | wx.TB_FLAT) 
  28         # But we're doing it a different way here. 
  30         log
.write("Default toolbar tool size: %s\n" % tb
.GetToolBitmapSize()) 
  32         self
.CreateStatusBar() 
  34         tb
.AddSimpleTool(10, images
.getNewBitmap(), "New", "Long help for 'New'") 
  35         #tb.AddLabelTool(10, "New", images.getNewBitmap(), shortHelp="New", longHelp="Long help for 'New'") 
  36         self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=10) 
  37         self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
, id=10) 
  39         tb
.AddSimpleTool(20, images
.getOpenBitmap(), "Open", "Long help for 'Open'") 
  40         self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=20) 
  41         self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
, id=20) 
  44         tb
.AddSimpleTool(30, images
.getCopyBitmap(), "Copy", "Long help for 'Copy'") 
  45         self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=30) 
  46         self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
, id=30) 
  48         tb
.AddSimpleTool(40, images
.getPasteBitmap(), "Paste", "Long help for 'Paste'") 
  49         self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=40) 
  50         self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
, id=40) 
  54         tool 
= tb
.AddCheckTool(50, images
.getTog1Bitmap(), 
  55                                shortHelp
="Toggle this") 
  56         self
.Bind(wx
.EVT_TOOL
, self
.OnToolClick
, id=50) 
  58 ##         tb.AddCheckTool(60, images.getTog1Bitmap(), images.getTog2Bitmap(), 
  59 ##                         shortHelp="Toggle with 2 bitmaps") 
  60 ##         self.Bind(EVT_TOOL, self.OnToolClick, id=60) 
  62         self
.Bind(wx
.EVT_TOOL_ENTER
, self
.OnToolEnter
) 
  63         self
.Bind(wx
.EVT_TOOL_RCLICKED
, self
.OnToolRClick
) # Match all 
  64         self
.Bind(wx
.EVT_TIMER
, self
.OnClearSB
) 
  71                 tb
, cbID
, "", choices
=["", "This", "is a", "wxComboBox"], 
  72                 size
=(150,-1), style
=wx
.CB_DROPDOWN
 
  75         self
.Bind(wx
.EVT_COMBOBOX
, self
.OnCombo
, id=cbID
) 
  76         tb
.AddControl(wx
.TextCtrl(tb
, -1, "Toolbar controls!!", size
=(150, -1))) 
  78         # Final thing to do for a toolbar is call the Realize() method. This 
  79         # causes it to render (more or less, that is). 
  83     def OnToolClick(self
, event
): 
  84         self
.log
.WriteText("tool %s clicked\n" % event
.GetId()) 
  85         tb 
= self
.GetToolBar() 
  86         tb
.EnableTool(10, not tb
.GetToolEnabled(10)) 
  88     def OnToolRClick(self
, event
): 
  89         self
.log
.WriteText("tool %s right-clicked\n" % event
.GetId()) 
  91     def OnCombo(self
, event
): 
  92         self
.log
.WriteText("combobox item selected: %s\n" % event
.GetString()) 
  94     def OnToolEnter(self
, event
): 
  95         self
.log
.WriteText('OnToolEnter: %s, %s\n' % (event
.GetId(), event
.GetInt())) 
  97         if self
.timer 
is None: 
  98             self
.timer 
= wx
.Timer(self
) 
 100         if self
.timer
.IsRunning(): 
 103         self
.timer
.Start(2000) 
 107     def OnClearSB(self
, event
):  # called for the timer event handler 
 108         self
.SetStatusText("") 
 113     def OnCloseWindow(self
, event
): 
 114         if self
.timer 
is not None: 
 119 #--------------------------------------------------------------------------- 
 121 def runTest(frame
, nb
, log
): 
 122     win 
= TestToolBar(frame
, log
) 
 126 #--------------------------------------------------------------------------- 
 131 wx.ToolBar is a narrow strip of icons on one side of a frame (top, bottom, sides) 
 132 that acts much like a menu does, except it is always visible. Additionally, actual 
 133 wxWindows controls, such as wx.TextCtrl or wx.ComboBox, can be added to the toolbar 
 134 and used from within it. 
 136 Toolbar creation is a two-step process. First, the toolbar is defined using the 
 137 various Add* methods of wx.ToolBar. Once all is set up, then wx.Toolbar.Realize() 
 138 must be called to render it. 
 140 wx.Toolbar events are also propogated as Menu events; this is especially handy when 
 141 you have a menu bar that contains items that carry out the same function. For example, 
 142 it is not uncommon to have a little 'floppy' toolbar icon to 'save' the current file  
 143 (whatever it is) as well as a FILE/SAVE menu item that does the same thing. In this 
 144 case, both events can be captured and acted upon using the same event handler 
 147 If there are cases where a toolbar icon should *not* be associated with a menu item, 
 148 use a unique ID to trap it. 
 150 There are a number of ways to create a toolbar for a wx.Frame. wx.Frame.CreateToolBar()  
 151 does all the work except it adds no buttons at all unless you override the virtual method 
 152 OnCreateToolBar(). On the other hand, you can just subclass wx.ToolBar and then use 
 153 wx.Frame.SetToolBar() instead. 
 155 Note that wx.TB_DOCKABLE is only supported under GTK. An attempt to alleviate this 
 156 is provided in wx.lib.floatbar, but it is not formally supported. 
 160 if __name__ 
== '__main__': 
 163     run
.main(['', os
.path
.basename(sys
.argv
[0])] + sys
.argv
[1:])