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

2024-05-26 07:48:16 发布

您现在位置:Python中文网/ 问答频道 /正文

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

frame.DefaultProperties = customProperties

frame.DefaultProperties.colBg = customColor

但两者都会抛出AttributeError(“无法设置属性”)。感谢您的帮助


Tags: 方法应用程序颜色记录framewx油漆getdefaultattributes
1条回答
网友
1楼 · 发布于 2024-05-26 07:48:16

默认属性可能是在为桌面设置的任何主题中定义的。我不相信有办法从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 hereenter image description here

相关问题 更多 >