xilpex
xilpex

Reputation: 3237

wxPython - A side toolbar

We all know that the wxPython toolbar is most of the time, if not always placed at the top. But is there a way to have it on the side (left preferably)? Is there a way to turn this code's (taken from here) top toolbar into a side toolbar:

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
ZetCode wxPython tutorial

This example creates a simple toolbar.

author: Jan Bodnar
website: www.zetcode.com
last modified: April 2018
"""

import wx


class Example(wx.Frame):

    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)

        self.InitUI()

    def InitUI(self):

        toolbar = self.CreateToolBar()
        qtool = toolbar.AddTool(wx.ID_ANY, 'Quit', wx.Bitmap('texit.png'))
        toolbar.Realize()

        self.Bind(wx.EVT_TOOL, self.OnQuit, qtool)

        self.SetSize((350, 250))
        self.SetTitle('Simple toolbar')
        self.Centre()

    def OnQuit(self, e):
        self.Close()


def main():

    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()


if __name__ == '__main__':
    main()

Upvotes: 0

Views: 596

Answers (1)

Rolf of Saxony
Rolf of Saxony

Reputation: 22443

Make use of the toolbar Style attributes.

import wx
class Example(wx.Frame):
    def __init__(self, *args, **kwargs):
        super(Example, self).__init__(*args, **kwargs)
        self.InitUI()

    def InitUI(self):
        toolbar = self.CreateToolBar(wx.TB_VERTICAL|wx.TB_TEXT)
        atool = toolbar.AddTool(wx.ID_ANY, 'Tool_A', wx.Bitmap('stop.png'))
        btool = toolbar.AddTool(wx.ID_ANY, 'Tool_B', wx.Bitmap('stop.png'))
        ctool = toolbar.AddTool(wx.ID_ANY, 'Quit', wx.Bitmap('stop.png'))
        toolbar.Realize()

        self.Bind(wx.EVT_TOOL, self.OnQuit, ctool)

        self.SetSize((350, 250))
        self.SetTitle('Simple toolbar')
        self.Centre()

    def OnQuit(self, e):
        self.Close()

def main():
    app = wx.App()
    ex = Example(None)
    ex.Show()
    app.MainLoop()

if __name__ == '__main__':
    main()

enter image description here

Styles available:

  • wx.TB_FLAT: Gives the toolbar a flat look (Windows and GTK only).
  • wx.TB_DOCKABLE: Makes the toolbar floatable and dockable (GTK only).
  • wx.TB_HORIZONTAL: Specifies horizontal layout (default).
  • wx.TB_VERTICAL: Specifies vertical layout.
  • wx.TB_TEXT: Shows the text in the toolbar buttons; by default only icons are shown.
  • wx.TB_NOICONS: Specifies no icons in the toolbar buttons; by default they are shown.
  • wx.TB_NODIVIDER: Specifies no divider (border) above the toolbar (Windows only)
  • wx.TB_NOALIGN: Specifies no alignment with the parent window (Windows only, not very useful).
  • wx.TB_HORZ_LAYOUT: Shows the text and the icons alongside, not vertically stacked (Windows and GTK 2 only). This style must be used with TB_TEXT .
  • wx.TB_HORZ_TEXT: Combination of TB_HORZ_LAYOUT and TB_TEXT .
  • wx.TB_NO_TOOLTIPS: Don’t show the short help tooltips for the tools when the mouse hovers over them.
  • wx.TB_BOTTOM: Align the toolbar at the bottom of parent window.
  • wx.TB_RIGHT: Align the toolbar at the right side of parent window.
  • wx.TB_DEFAULT_STYLE: Combination of TB_HORIZONTAL and TB_FLAT . This style is new since wxWidgets 2.9.5.

Upvotes: 2

Related Questions