wxpython:设置应用程序颜色(默认属性)

问题描述 投票:1回答:1

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

frame.DefaultProperties = customProperties

frame.DefaultProperties.colBg = customColor

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

python wxpython
1个回答
0
投票

默认属性可能在您为桌面设置的任何主题内定义。我不相信有一种方法可以从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()

[enter image description here enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.