wxPython之SystemSettings

SystemSettings

  SystemSettings对象允许程序查询系统默认的颜色和字体信息。在创建自定义绘制控件时,能够知道这些信息是非常有用的。它使得使用和系统组件正在使用的一样的颜色和字体称为可能。这使自定义的控件或窗口装饰和系统组件无违和,看起来和其他原始的系统组件一样共享同一窗口。

示例说明

创建一个控件类似StaticText,但是有一个和Frame的标题栏相似的标题。

#-*-coding: UTF-8 -*-
#------------------------------------------------------
#Purpose: nothing....

#Author: 阿Bin先生
#Created: 2017年5月21日
#------------------------------------------------------
import wx

class CaptionBox(wx.PyPanel):
    def __init__(self, parent, caption):
        super(CaptionBox, self).__init__(parent, style=wx.NO_BORDER)
        # Attributes
        self._caption = caption
        self._csizer = wx.BoxSizer(wx.VERTICAL)
        # Setup
        self.__DoLayout()
        # Event Handlers
        self.Bind(wx.EVT_PAINT, self.OnPaint)

    def __DoLayout(self):
        msizer = wx.BoxSizer(wx.HORIZONTAL)
        self._csizer.AddSpacer(12) # extra space for caption
        msizer.Add(self._csizer, 0, wx.EXPAND|wx.ALL, 8)
        self.SetSizer(msizer)

    def DoGetBestSize(self):
        size = super(CaptionBox, self).DoGetBestSize()
        # Compensate for wide caption labels
        tw = self.GetTextExtent(self._caption)[0]
        size.SetWidth(max(size.width, tw+20))
        return size

    def AddItem(self, item):
        """Add a window or sizer item to the CaptionBox"""
        self._csizer.Add(item, 0, wx.ALL, 5)

    def OnPaint(self, event):
        """Draws the Caption and border around the controls"""
        dc = wx.PaintDC(self)
        # Get the working rectangle we can draw in
        rect = self.GetClientRect()
        # Get the system color to draw the caption
        ss = wx.SystemSettings
        color = ss.GetColour(wx.SYS_COLOUR_ACTIVECAPTION)
        txtcolor = ss.GetColour(wx.SYS_COLOUR_CAPTIONTEXT)
        dc.SetTextForeground(txtcolor)
        # Draw the border
        rect.Inflate(-2, -2)
        dc.SetPen(wx.Pen(color))
        dc.SetBrush(wx.TRANSPARENT_BRUSH)
        dc.DrawRectangleRect(rect)
        # Add the Caption
        rect = wx.Rect(rect.x, rect.y,
        rect.width, 16)
        dc.SetBrush(wx.Brush(color))
        dc.DrawRectangleRect(rect)
        rect.Inflate(-5, 0)
        dc.SetFont(self.GetFont())
        dc.DrawLabel(self._caption, rect, wx.ALIGN_LEFT)

class MyFrame(wx.Frame):
    def __init__(self, parent, *args, **kwargs):
        super(MyFrame, self).__init__(parent, *args, **kwargs)
        # Attributes
        self.Panel = wx.Panel(self)
        box = CaptionBox(self.Panel, "CaptionBox")
        box.SetSize([100, 100])

class MyApp(wx.App):
    def OnInit(self):
        self.frame = MyFrame(None, title="DrawShapes",size = [500, 500])
        self.SetTopWindow(self.frame)
        self.frame.Show()
        return True

if __name__ == "__main__":
    app = MyApp(False)
    app.MainLoop()        

运行结果:

这里写图片描述

书中的截图更表达清楚:
这里写图片描述

示例分析

在OnPaint方法里,利用从SystemSettings单实例获得的颜色,绘制控件标题的背景颜色和文本颜色。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值