wxpython基本对象wx.colour颜色选择器

发布时间:2021-01-17编辑:脚本学堂
有关wxpython基本对象之颜色选择器的用法,通过二个例子介绍了wx.Colour颜色对象的用法,WxPython中预定义的颜色名字、标准颜色数据库等。

颜色是一个表示结合了不同强度值(intensity value)的红光(Red)、绿光(Green)以及蓝光(Blue)的对象. RGB 的有效值是 0-255 的整数值.
有三种方式来设置颜色。

可以创建一个 wx.Colour 对象,也可以使用一个预定义的颜色名字,还可以使用一个十六进制的字符串。

专题教程:wxpython中文教程

例子:
 

复制代码 代码示例:
SetBackgroundColour(wx.Colour(0, 0, 255))
SetBackgroundColour('BLUE')
SetBackgroundColour('#0000FF')

WxPython中预定义的颜色名字有:
wx.BLACK
wx.WHITE
wx.RED
wx.BLUE
wx.GREEN
wx.CYAN
wx.LIGHT_GREY

标准颜色数据库
 

AQUAMARINE
CORNFLOWER BLUE
DARK SLATE GREY
GREY
LIGHT STEEL BLUE
MEDIUM GOLDENROD
MIDNIGHT BLUE
PLUM
SLATE BLUE
VIOLET RED
BLACK
CYAN
DARK TURQUOISE
GREEN
LIME GREEN
MEDIUM ORCHID
NAVY
PURPLE
SPRING GREEN
WHEAT
BLUE
DARK GREY
DIM GREY
GREEN YELLOW
MAGENTA
MEDIUM SEA GREEN
ORANGE
RED
STEEL BLUE
WHITE
BLUE VIOLET
DARK GREEN
FIREBRICK
INDIAN RED
MAROON
MEDIUM SLATE BLUE
ORANGE RED
SALMON
TAN
YELLOW
BROWN
DARK OLIVE GREEN
FOREST GREEN
KHAKI
MEDIUM AQUAMARINE
MEDIUM SPRING GREEN
ORCHID
SEA GREEN
THISTLE
YELLOW GREEN
CADET BLUE
DARK ORCHID
GOLD
LIGHT BLUE
MEDIUM BLUE
MEDIUM TURQUOISE
PALE GREEN
SIENNA
TURQUOISE
CORAL
DARK SLATE BLUE
GOLDENROD
LIGHT GREY
MEDIUM FOREST GREEN
MEDIUM VIOLET RED
PINK
SKY BLUE
VIOLET
 

这个 colours.py 程序展示了 8 种不同的颜色. wx.BLACK 简单. 海洋蓝富有诗意.
而 #0000FF 则显得十分具有技术感. 选择何种颜色,取决于开发者本人.

例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

#colours.py

import wx
class Colours(wx.Dialog):
    def __init__(self, parent, id, title):
        wx.Dialog.__init__(self, parent, id, title, size=(300, 300))
        vbox = wx.BoxSizer(wx.VERTICAL)
        self.pnl1 = wx.Panel(self, -1)
        self.pnl2 = wx.Panel(self, -1)
        self.pnl3 = wx.Panel(self, -1)
        self.pnl4 = wx.Panel(self, -1)
        self.pnl5 = wx.Panel(self, -1)
        self.pnl6 = wx.Panel(self, -1)
        self.pnl7 = wx.Panel(self, -1)
        self.pnl8 = wx.Panel(self, -1)

        gs = wx.GridSizer(
        gs.AddMany([
                    (self.pnl1, 0, wx.EXPAND),
                    (self.pnl2, 0, wx.EXPAND),
                    (self.pnl3, 0, wx.EXPAND),
                    (self.pnl4, 0, wx.EXPAND),
                    (self.pnl5, 0, wx.EXPAND),
                    (self.pnl6, 0, wx.EXPAND),
                    (self.pnl7, 0, wx.EXPAND),
                    (self.pnl8, 0, wx.EXPAND)
                    ])
       
        vbox.Add(gs, 1, wx.EXPAND | wx.TOP, 5)
        self.SetSizer(vbox)
        self.SetColors()
        self.Center()
        self.ShowModal()
        self.Destroy()
       
    def SetColors(self):
        self.pnl1.SetBackgroundColour(wx.BLACK)
        self.pnl2.SetBackgroundColour(wx.Color(139, 105, 20))
        self.pnl3.SetBackgroundColour(wx.RED)
        self.pnl4.SetBackgroundColour('#0000FF')
        self.pnl5.SetBackgroundColour('sea green')
        self.pnl6.SetBackgroundColour('midnight blue')
        self.pnl7.SetBackgroundColour(wx.LIGHT_GREY)
        self.pnl8.SetBackgroundColour('plum')
       
app = wx.App(0)
Colours(None, -1, 'colours.py')
app.MainLoop()

如图:
 

wxpython wx.colour颜色选择器

图:colours.py
完整的颜色数据库包含了大约 630 种不同的颜色名字.
该清单可在 colourdb.py 程序文件中找到.wxPython 的演示程序中也有展示.
下面的 randocolours.py 程序中有一个独立窗口. 我们将随机选择颜色来作为该窗口的背景颜色.

例子:
 

复制代码 代码示例:

#!/usr/bin/python
#coding=utf-8

#randomcolours.py

import wx

from random import randrange
from wx.lib.colourdb import *

class MyFrame(wx.Frame):
    def __init__(self, parent, id, title):
        wx.Frame.__init__(self, parent, id, title, wx.DefaultPosition,          wx.Size(400, 350))
       
        self.panel = wx.Panel(self, -1)
        self.colors = getColourList()
        self.timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.OnTimer, self.timer)
        self.timer.Start(500)
        self.col_num = len(self.colors)
        self.Center()
       
    def OnTimer(self, event):
        self.panel.SetBackgroundColour(wx.RED)
        position = randrange(0, self.col_num-1, 1)
        self.panel.SetBackgroundColour(self.colors[position])
        self.panel.Refresh()
       
class MyApp(wx.App):
    def OnInit(self):
        updateColourDB()
        frame = MyFrame(None, -1, 'randomcolours.py')
        frame.Show(True)
        self.SetTopWindow(frame)
        return True
   
app = MyApp(0)
app.MainLoop()