Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I change the look and feel of the wxPython tool bar?

The wxPython ToolBar look and feel does not match that of the current operating system - it has a gradient similar to the Windows Vista / 7 menubar I.E. a silver gradient.

Is there any way to change this so that it blends in with the operating systems look and feel?

Note: There is a style flag that can be set when creating the ToolBar and one of those flags is wx.TB_FLAT but this seems to have no affect on the way the ToolBar is rendered.

I am running my wxPython program on Windows 7.

Edit: Below is a screen shot of what I am seeing.

alt text

Edit: It seems the toolbar is drawn in accordance with the current theme as changing to the Windows Classic theme renders a flat toolbar which matches the window background.

The code below shows what I have tried so far. I have created a method called OnPaint which is bound to the toolbars paint event. This has no effect and the toolbar is drawn as in the image above.

I know that the code in OnPaint works as the rectangle is rendered if i bind this method to the windows paint event instead of the toolbars.

import wx

ID_STAT = 1
ID_TOOL = 2

class CheckMenuItem(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, size=(350, 250))

        menubar = wx.MenuBar()
        file = wx.Menu()
        view = wx.Menu()
        self.shst = view.Append(ID_STAT, 'Show statubar', 'Show Statusbar', kind=wx.ITEM_CHECK)
        self.shtl = view.Append(ID_TOOL, 'Show toolbar', 'Show Toolbar', kind=wx.ITEM_CHECK)
        view.Check(ID_STAT, True)
        view.Check(ID_TOOL, True)

        self.Bind(wx.EVT_MENU, self.ToggleStatusBar, id=ID_STAT)
        self.Bind(wx.EVT_MENU, self.ToggleToolBar, id=ID_TOOL)

        menubar.Append(file, '&File')
        menubar.Append(view, '&View')
        self.SetMenuBar(menubar)

        self.toolbar = self.CreateToolBar()
        self.toolbar.Realize()

        self.statusbar = self.CreateStatusBar()

        self.Bind(wx.EVT_PAINT, self.OnPaint, self.toolbar)

        self.Centre()
        self.Show(True)

    def OnPaint(self, e):
        dc = wx.PaintDC(self)
        dc.SetBrush(wx.Brush('#c56c00'))
        dc.DrawRectangle(10, 15, 90, 60)

    def ToggleStatusBar(self, event):
        if self.shst.IsChecked():
            self.statusbar.Show()
        else:
            self.statusbar.Hide()

    def ToggleToolBar(self, event):
        if self.shtl.IsChecked():
            self.toolbar.Show()
        else:
            self.toolbar.Hide()

app = wx.App()
CheckMenuItem(None, -1, 'Toolbar Test')
app.MainLoop()
like image 875
Benjamin Gale Avatar asked Jan 16 '11 10:01

Benjamin Gale


1 Answers

It seems the solution to my problem is actually quite simple. Instead of trying to apply some custom paint logic all that was required was a call to the toolbars SetBackgroundColour() method.

The system look and feel can be maintained by using the colours from the wx.SystemSettings class.

self.toolbar.SetBackgroundColour(wx.SystemSettings.GetColour(wx.SYS_COLOUR_MENUBAR))

enter image description here

like image 104
Benjamin Gale Avatar answered Nov 04 '22 14:11

Benjamin Gale