wxPython GetBackgroundColour() function works diff

2019-03-06 01:17发布

问题:

I want to create an image in a wx Python application with the colour of the background.

[EDIT] On Windows this works perfectly:

[/EDIT]

but on linux my code gives a paler colour. What am I doing wrong?

[EDIT: more information]

The colour returned by self.GetBackgroundColour() is (225, 225, 225); the paler colour. The actual background colour is (212, 212, 212)

[\EDIT]

Here is an image taken using a different theme:

So based on Rolf's answer below it looks like an issue with Mate and not the theme

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Image')
        sizer = wx.BoxSizer()

        static_bitmap_A = wx.StaticBitmap(self, wx.ID_ANY)
        bitmap = wx.Bitmap('any.png')
        static_bitmap_A.SetBitmap(bitmap)
        sizer.Add(static_bitmap_A, flag=wx.ALL, border=10)

        image = wx.Image('any.png')
        colour = self.GetBackgroundColour()
        red, green, blue = colour[0], colour[1], colour[2]
        #red, green, blue = 0, 0, 0
        for row in range(image.GetSize()[0]):
            for column in range(image.GetSize()[1]):
                image.SetRGB(row, column, red, green, blue)
        bitmap = wx.Bitmap(image)
        static_bitmap_B = wx.StaticBitmap(self, wx.ID_ANY)
        static_bitmap_B.SetBitmap(bitmap)
        sizer.Add(static_bitmap_B, flag=wx.ALL, border=10)

        self.SetSizerAndFit(sizer)
        self.Show()

if __name__ == '__main__':
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

Any image can be used in place of any.png

回答1:

This is just to back up my original comments.
I can only assume that your issue is something to do with your Theme or other setup you have on your box, although I of course reserve the right to be horribly wrong.
This code, on Mint 19 (Ubuntu 18.04) Python 3.6 Wx 4.0.3 gtk2

import wx

class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, -1, 'Image')
        sizer = wx.BoxSizer()

        sys_background = wx.SystemSettings.GetColour(wx.SYS_COLOUR_BACKGROUND)
        print("default colour ", sys_background)

        static_bitmap_A = wx.StaticBitmap(self, wx.ID_ANY)
        bitmap = wx.Bitmap('/home/rolf/any2.png')
        static_bitmap_A.SetBitmap(bitmap)
        sizer.Add(static_bitmap_A, flag=wx.ALL, border=10)

        image = wx.Image('/home/rolf/any2.png')
        #colour = self.GetBackgroundColour()
        colour = sys_background
        red, green, blue = colour[0], colour[1], colour[2]
        print(red, green, blue)
        for row in range(image.GetSize()[0]):# -1 ):
            for column in range(image.GetSize()[1]):
                image.SetRGB(row, column, red, green, blue)
        bitmap = wx.Bitmap(image)
        static_bitmap_B = wx.StaticBitmap(self, wx.ID_ANY,bitmap)
        sizer.Add(static_bitmap_B, flag=wx.ALL, border=10)

        self.SetSizerAndFit(sizer)
        self.Show()

if __name__ == '__main__':
    screen_app = wx.App()
    main_frame = MainFrame()
    screen_app.MainLoop()

Outputs (Theme Mint-X):

default colour  (214, 214, 214, 255)
214 214 214

This continues to work properly, when changing the Theme, simply outputting different numbers for the colour values.

Theme Mint-Y

default colour  (240, 240, 240, 255)
240 240 240

It works whether using colour = self.GetBackgroundColour() or colour = sys_background