How to change the appearance of the wxPython toolbar?

The appearance of the wxPython ToolBar does not match the appearance of the current operating system - it has a gradient similar to the Windows Vista / 7 IE silver gradient menu.

Is there a way to change this so that it fits into the operating systems and looks?

Note. There is a style flag that you can set when creating the ToolBar, and one of these flags is wx.TB_FLAT, but this does not seem to affect the way the ToolBar is displayed.

I am running my wxPython program on Windows 7.

Edit: Below is a screenshot of what I see.

alt text

Edit: it seems the toolbar is being drawn in accordance with the current theme, since a change to the Windows Classic theme makes the toolbar flat that matches the background of the window.

The code below shows what I have tried so far. I created a method called OnPaint associated with the paintbars event. This does not affect, and the toolbar is drawn in the same way as in the image above.

I know the code in OnPaint works because the rectangle is rendered if I attach this method to the paint paint event instead of 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() 
+4
source share
2 answers

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

System appearance can be maintained using colors from the wx.SystemSettings class.

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

enter image description here

+3
source

Does the wxPython demo have the same behavior? I donโ€™t remember that it didnโ€™t look right on my Windows 7 machine. Perhaps the manifest file is not installed.

0
source

Source: https://habr.com/ru/post/1335718/


All Articles