Looping over toolbar tools

Hello,

I want to loop over all tools in a wx.ToolBar to disable each. Unfortunately I can't find a way to get the number of tools. Is there any way to do that?

-Matthias

Hello Matthias,

I want to loop over all tools in a wx.ToolBar to disable each.
Unfortunately I can't find a way to get the number of tools. Is there any
way to do that?

Well, you could do a couple of things:

1) If you use a fixed id for every toolbar button/control, you could
loop through that fixed values and disable the tools. I.e.:

ID_FirstButton = wx.NewId()
ID_SecondButton = wx.NewId()
ID_ThirdButton = wx.NewId()
# and so on

# ...later on, in the code

toolbar.AddLabelTool(ID_FirstButton, "Item 1", bmp1)
toolbar.AddLabelTool(ID_SecondButton, "Item 2", bmp2)
toolbar.AddLabelTool(ID_ThirdButton, "Item 3", bmp3)

# ... later on, in the disabling code:

for ids in xrange(ID_FirstButton, ID_NthButton):
    toolbar.EnableTool(ids, False)

2) If you construct your toolbar entirely in one single
method/function (i.e., you don't add tools later or you do not remove
tools later), you could do something like:

def BuildMyToolbar(self):
    ID_FirstButton = wx.NewId()
    toolbar.AddLabelTool(ID_FirstButton, "Item 1", bmp1)
    toolbar.AddLabelTool(wx.NewId(), "Item 2", bmp2)
    toolbar.AddLabelTool(wx.NewId(), "Item 3", bmp3)
    # ... a lot more tools

    ID_NthButton = wx.NewId()
    toolbar.AddLabelTool(ID_NthButton, "Item N", bmpN)

    self.toolbarRange = [ID_FirstButton, ID_NthButton]

# ... later on, in the disabling code:

for ids in xrange(self.toolbarRange[0], self.toolbarRange[1]):
    toolbar.EnableTool(ids, False)

Or something like that. I have not tested it, and there might be a
thousand of other simpler/smarter/faster ways to do it. Sorry if
doesn't help a lot.

···

--
Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/

Thanks for your help. I am using XRC lately (I like it really better for doing the layout of static widgets) and I am ot sure if I can make your approach work with it easily. I don't want to call XRCID with every xml identifier manually. I guess I could access the xrc xml nodes directly somehow (judging from xrc.py), but that also seems to be ugly. Maybe I could also subclass the toolbar class and hook the addtool method or something. Not sure if this works at all.
I feel there should be a simpler way than all this clumsy stuff to get at all toolbar tools.

-Matthias

···

Am 18.10.2006, 22:35 Uhr, schrieb Andrea Gavana <andrea.gavana@gmail.com>:

Hello Matthias,

I want to loop over all tools in a wx.ToolBar to disable each.
Unfortunately I can't find a way to get the number of tools. Is there any
way to do that?

Well, you could do a couple of things:

1) If you use a fixed id for every toolbar button/control, you could
loop through that fixed values and disable the tools. I.e.:

ID_FirstButton = wx.NewId()
ID_SecondButton = wx.NewId()
ID_ThirdButton = wx.NewId()
# and so on

# ...later on, in the code

toolbar.AddLabelTool(ID_FirstButton, "Item 1", bmp1)
toolbar.AddLabelTool(ID_SecondButton, "Item 2", bmp2)
toolbar.AddLabelTool(ID_ThirdButton, "Item 3", bmp3)

# ... later on, in the disabling code:

for ids in xrange(ID_FirstButton, ID_NthButton):
    toolbar.EnableTool(ids, False)

2) If you construct your toolbar entirely in one single
method/function (i.e., you don't add tools later or you do not remove
tools later), you could do something like:

def BuildMyToolbar(self):
    ID_FirstButton = wx.NewId()
    toolbar.AddLabelTool(ID_FirstButton, "Item 1", bmp1)
    toolbar.AddLabelTool(wx.NewId(), "Item 2", bmp2)
    toolbar.AddLabelTool(wx.NewId(), "Item 3", bmp3)
    # ... a lot more tools

    ID_NthButton = wx.NewId()
    toolbar.AddLabelTool(ID_NthButton, "Item N", bmpN)

    self.toolbarRange = [ID_FirstButton, ID_NthButton]

# ... later on, in the disabling code:

for ids in xrange(self.toolbarRange[0], self.toolbarRange[1]):
    toolbar.EnableTool(ids, False)

Or something like that. I have not tested it, and there might be a
thousand of other simpler/smarter/faster ways to do it. Sorry if
doesn't help a lot.

Each of the Add*Tool functions returns an instance of the object representing your tool. You could assign a reference to each tool to a list as you define them. It might look something like this (a quick modification of one of Andrea's suggestions, untested):

def BuildMyToolbar(self):
    toolbar = self.CreateToolBar(wx.TB_HORIZONTAL|wx.TB_FLAT)
    tools =
    tools.append(toolbar.AddLabelTool(wx.NewId(), "Item 1", bmp1))
    tools.append(toolbar.AddLabelTool(wx.NewId(), "Item 2", bmp2))
    tools.append(toolbar.AddLabelTool(wx.NewId(), "Item 3", bmp3))
    # ... a lot more tools
    self.tools = tools

And when you're ready to loop over them just stick self.tools into a for loop:

def ListMyTools(self):
    for tool in self.tools:
        print repr(tool)

Robert O'Connor

···

----- Original Message ----- From: "Nitro" <nitro@dr-code.org>
To: <wxPython-users@lists.wxwidgets.org>
Sent: Wednesday, October 18, 2006 2:57 PM
Subject: [wxPython-users] Looping over toolbar tools

Hello,

I want to loop over all tools in a wx.ToolBar to disable each. Unfortunately I can't find a way to get the number of tools. Is there any way to do that?

-Matthias