【问题标题】:wxpython: Set application color (Default Properties)wxpython:设置应用程序颜色(默认属性)
【发布时间】:2020-06-08 04:58:50
【问题描述】:

我想更改整个 pythonwx 应用程序的颜色。我发现当前使用的颜色分别记在 wx.Frame.DefaultAttributes.colBg.colFg 中。我用油漆检查了这些确实是使用过的颜色。 现在有一个 wx.Frame.GetDefaultAttributes() 但没有 wx.Frame.SetDefaultAttributes() 方法。但是我仍然需要更改颜色,并且我认为手动设置每个控件并不是理想的解决方案。 我试过了:

frame.DefaultProperties = customProperties

frame.DefaultProperties.colBg = customColor

但两者都抛出 AttributeError(“无法设置属性”)。任何帮助表示赞赏。

【问题讨论】:

    标签: python wxpython


    【解决方案1】:

    默认属性可能在您为桌面设置的任何主题中定义。我不相信有办法从 wxpython 中重新定义这些。

    我发现设置默认配色方案的最简单方法是为对象(例如面板)中的每个子项设置颜色。

    在下面的代码中,一直按Encrypt 按钮以查看结果。

    import wx
    from random import randrange
    
    class CipherTexter(wx.Frame):
        def __init__(self, parent, title):
            wx.Frame.__init__(self, parent, title=title,  size=(1000, 600))
            self.panel = wx.Panel(self)
            cipherText = wx.StaticText(self.panel, label="Cipher Texter ", pos=(20, 30))
            encryptorText = wx.StaticText(self.panel, label="Encryptor ", pos=(20, 70))
            decryptorText = wx.StaticText(self.panel, label="Decryptor ", pos=(20, 100))
            self.cipher = wx.TextCtrl(self.panel, -1, style=wx.TE_MULTILINE, size=(400,400), pos=(400, 30))
            self.encryptor = wx.TextCtrl(self.panel, -1, size=(100,30), pos=(200, 70))
            self.decryptor = wx.TextCtrl(self.panel, -1, size=(100,30), pos=(200, 100))
            self.encrypt = wx.Button(self.panel, -1, "Encrypt", pos=(20, 140))
            self.decrypt = wx.Button(self.panel, -1, "Decrypt", pos=(20, 180))
            self.panel.SetBackgroundColour('white')
            self.encrypt.Bind(wx.EVT_BUTTON, self.encryptNow)
            self.decrypt.Bind(wx.EVT_BUTTON, self.decryptNow)
            self.Show()
    
        def AColour(self):
            red = randrange(0,255)
            green = randrange(0,255)
            blue = randrange(0,255)
            x = wx.Colour(red,green,blue)
            return x
    
        def encryptNow(self, event):
            cfg_colour = self.AColour()
            txt_colour = self.AColour()
            children = self.panel.GetChildren()
            for child in children:
                child.SetBackgroundColour(cfg_colour)
                child.SetForegroundColour(txt_colour)
            print(cfg_colour)
    
        def decryptNow(self, event):
            pass
    
    app = wx.App(False)
    frame = CipherTexter(None, "The SS Cipher")
    app.MainLoop()
    

    【讨论】:

    • 谢谢。遍历孩子并为每个孩子着色可能是最好的方法。不幸的是,使用此方法,您无法设置某些控件,例如 ToggleButton(设置背景颜色只会更改按钮周围的小边缘)。
    • 这将限制小部件或您的桌面主题。设置子小部件的背景颜色与单独为小部件本身设置背景颜色相同。毕竟是同一个小部件。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-08
    相关资源
    最近更新 更多