PyGobject(一百零八)CSS系列——混合模式

  • 例子

混合模式决定这两个图片混合在一起后,显示的样子
在css中使用“background-blend-mode”属性设置

例子

PyGobject(一百零八)CSS系列——混合模式_第1张图片
PyGobject(一百零八)CSS系列——混合模式_第2张图片

代码:

#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/19
# section 158
# 
# author: xiaosanyu
# website: yuxiaosan.tk \
#          http://blog.csdn.net/a87b01c14
# created: 16/7/19

TITLE = "Blendmodes"
DESCRIPTION = """
You can blend multiple backgrounds using the CSS blend modes available.
"""

import gi

gi.require_version("Gtk", "3.0")
from gi.repository import Gtk, Gdk, GLib, Gio, GObject
import os

blend_modes = [
    {"name": "Color", "id": "color"},
    {"name": "Color (burn)", "id": "color-burn"},
    {"name": "Color (dodge)", "id": "color-dodge"},
    {"name": "Darken", "id": "darken"},
    {"name": "Difference", "id": "difference"},
    {"name": "Exclusion", "id": "exclusion"},
    {"name": "Hard Light", "id": "hard-light"},
    {"name": "Hue", "id": "hue"},
    {"name": "Lighten", "id": "lighten"},
    {"name": "Luminosity", "id": "luminosity"},
    {"name": "Multiply", "id": "multiply"},
    {"name": "Normal", "id": "normal"},
    {"name": "Overlay", "id": "overlay"},
    {"name": "Saturate", "id": "saturate"},
    {"name": "Screen", "id": "screen"},
    {"name": "Soft Light", "id": "soft-light"},
]


class CSSBlendmodesWindow():
    def __init__(self):
        builder = Gtk.Builder.new_from_resource("/css/blendmodes.glade")
        window = builder.get_object("window")
        window.connect("destroy", Gtk.main_quit)

        # Setup the CSS provider for window 
        provider = Gtk.CssProvider()
        Gtk.StyleContext.add_provider_for_screen(Gdk.Screen.get_default(),
                                                 provider,
                                                 Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

        self.setup_listbox(builder, provider)
        window.show_all()
        Gtk.main()

    def update_css_for_blend_mode(self, provider, blend_mode):
        bytes = Gio.resources_lookup_data("/css/css_blendmodes.css", 0)
        css = bytes.get_data().decode('utf-8') % (blend_mode, blend_mode, blend_mode)
        try:
            provider.load_from_data(css.encode('utf-8'))
        except GLib.GError as e:
            print(e)
            if e.domain != 'gtk-css-provider-error-quark':
                raise e

    def row_activated(self, listbox, row, provider):
        blend_mode = blend_modes[row.get_index()]["id"]
        self.update_css_for_blend_mode(provider, blend_mode)

    def setup_listbox(self, builder, provider):
        listbox = Gtk.ListBox()
        builder.get_object("scrolledwindow").add(listbox)

        listbox.connect("row-activated", self.row_activated, provider)

        # Add a row for each blend mode available
        for mode in blend_modes:
            label = Gtk.Label(label=mode["name"])
            label.set_xalign(0)
            row = Gtk.ListBoxRow()
            row.add(label)
            listbox.add(row)
            # The first selected row is "normal" 
            if mode["id"] == "normal":
                normal_row = row

        # Select the "normal" row
        listbox.select_row(normal_row)
        normal_row.emit("activate")

        normal_row.grab_focus()


def main():
    CSSBlendmodesWindow()


if __name__ == "__main__":
    base_path = os.path.abspath(os.path.dirname(__file__))
    resource_path = os.path.join(base_path, '../Data/demo.gresource')
    resource = Gio.Resource.load(resource_path)
    Gio.resources_register(resource)
    main()

布局文件blendmodes.glade

version="1.0" encoding="UTF-8"?>

  "gtk+" version="3.20"/>
  class="GtkWindow" id="window">
    <property name="can_focus">Falseproperty>
    <property name="resizable">Falseproperty>
    <property name="title">CSS Blend Modesproperty>
    <property name="default_width">400property>
    <property name="default_height">300property>
    
      class="GtkGrid">
        <property name="visible">Trueproperty>
        <property name="can_focus">Falseproperty>
        <property name="border_width">12property>
        <property name="row_spacing">12property>
        <property name="column_spacing">12property>
        
          class="GtkLabel">
            <property name="visible">Trueproperty>
            <property name="can_focus">Falseproperty>
            <property name="label" translatable="yes">Blend mode:property>
            <property name="xalign">0property>
            
          
          
            <property name="left_attach">0property>
            <property name="top_attach">0property>
          
        
        
          class="GtkScrolledWindow" id="scrolledwindow">
            <property name="visible">Trueproperty>
            <property name="can_focus">Trueproperty>
            <property name="vexpand">Trueproperty>
            <property name="shadow_type">inproperty>
            <property name="min_content_width">150property>
          
          
            <property name="left_attach">0property>
            <property name="top_attach">1property>
          
        
        
          class="GtkStackSwitcher">
            <property name="visible">Trueproperty>
            <property name="can_focus">Falseproperty>
            <property name="halign">centerproperty>
            <property name="hexpand">Trueproperty>
            <property name="stack">stackproperty>
          
          
            <property name="left_attach">1property>
            <property name="top_attach">0property>
          
        
        
          class="GtkStack" id="stack">
            <property name="visible">Trueproperty>
            <property name="can_focus">Falseproperty>
            <property name="hexpand">Trueproperty>
            <property name="vexpand">Trueproperty>
            <property name="hhomogeneous">Falseproperty>
            <property name="vhomogeneous">Falseproperty>
            <property name="transition_type">crossfadeproperty>
            
              class="GtkGrid">
                <property name="visible">Trueproperty>
                <property name="can_focus">Falseproperty>
                <property name="halign">centerproperty>
                <property name="valign">centerproperty>
                <property name="hexpand">Falseproperty>
                <property name="vexpand">Trueproperty>
                <property name="row_spacing">12property>
                <property name="column_spacing">12property>
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">Duckproperty>
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">0property>
                  
                
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">Backgroundproperty>
                  
                  
                    <property name="left_attach">1property>
                    <property name="top_attach">0property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">1property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    
                  
                  
                    <property name="left_attach">1property>
                    <property name="top_attach">1property>
                  
                
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">
Blended pictureproperty>
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">2property>
                    <property name="width">2property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="halign">centerproperty>
                    
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">3property>
                    <property name="width">2property>
                  
                
              
              
                <property name="name">page0property>
                <property name="title" translatable="yes">Duckyproperty>
              
            
            
              class="GtkGrid">
                <property name="visible">Trueproperty>
                <property name="can_focus">Falseproperty>
                <property name="halign">centerproperty>
                <property name="valign">centerproperty>
                <property name="hexpand">Falseproperty>
                <property name="vexpand">Trueproperty>
                <property name="row_spacing">12property>
                <property name="column_spacing">12property>
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">Redproperty>
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">0property>
                  
                
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">Blueproperty>
                  
                  
                    <property name="left_attach">1property>
                    <property name="top_attach">0property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">1property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    
                  
                  
                    <property name="left_attach">1property>
                    <property name="top_attach">1property>
                  
                
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">
Blended pictureproperty>
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">2property>
                    <property name="width">2property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="halign">centerproperty>
                    
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">3property>
                    <property name="width">2property>
                  
                
              
              
                <property name="name">page1property>
                <property name="title" translatable="yes">Blendsproperty>
              
            
            
              class="GtkGrid">
                <property name="visible">Trueproperty>
                <property name="can_focus">Falseproperty>
                <property name="halign">centerproperty>
                <property name="valign">centerproperty>
                <property name="hexpand">Trueproperty>
                <property name="vexpand">Trueproperty>
                <property name="row_spacing">6property>
                <property name="column_spacing">12property>
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">1property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    
                  
                  
                    <property name="left_attach">1property>
                    <property name="top_attach">1property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">3property>
                  
                
                
                  class="GtkImage">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="halign">centerproperty>
                    
                  
                  
                    <property name="left_attach">1property>
                    <property name="top_attach">3property>
                  
                
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">Cyanproperty>
                    <property name="xalign">0property>
                    
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">0property>
                  
                
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">Magentaproperty>
                    <property name="xalign">0property>
                    
                  
                  
                    <property name="left_attach">1property>
                    <property name="top_attach">0property>
                  
                
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">Yellowproperty>
                    <property name="xalign">0property>
                    
                  
                  
                    <property name="left_attach">0property>
                    <property name="top_attach">2property>
                  
                
                
                  class="GtkLabel">
                    <property name="visible">Trueproperty>
                    <property name="can_focus">Falseproperty>
                    <property name="label" translatable="yes">Blended pictureproperty>
                    <property name="xalign">0property>
                    
                      name="weight" value="bold"/>
                    
                  
                  
                    <property name="left_attach">1property>
                    <property name="top_attach">2property>
                  
                
              
              
                <property name="name">page2property>
                <property name="title" translatable="yes">CMYKproperty>
              
            
          
          
            <property name="left_attach">1property>
            <property name="top_attach">1property>
          
        
      
    
    "titlebar">
      
    
  

样式文件css_blendmodes.css

/*
 * First page.
 */
image.duck {
    background-image: url('resource://css/ducky.png');
    background-size: cover;
    min-width: 200px;
    min-height: 200px;
}

image.gradient {
    background-image: linear-gradient(to right, red 0%%, green 50%%, blue 100%%);
    min-width: 200px;
    min-height: 200px;
}

/*
 * Second page.
 */
image.red {
    background: url('resource://css/blends.png') top center;
    min-width: 200px;
    min-height: 200px;
}

image.blue {
    background: url('resource://css/blends.png') bottom center;
    min-width: 200px;
    min-height: 200px;
}

/*
 * Third page.
 */
image.cyan {
    background: url('resource://css/cmy.jpg') top center;
    min-width: 200px;
    min-height: 200px;
}

image.magenta {
    background: url('resource://css/cmy.jpg') center center;
    min-width: 200px;
    min-height: 200px;
}

image.yellow {
    background: url('resource://css/cmy.jpg') bottom center;
    min-width: 200px;
    min-height: 200px;
}

image.blend0 {
  background-image: url('resource://css/ducky.png'),
                    linear-gradient(to right, red 0%%, green 50%%, blue 100%%);
  background-size: cover;
  background-blend-mode: %s;
  min-width: 200px;
  min-height: 200px;
}

image.blend1 {
  background: url('resource://css/blends.png') top center,
              url('resource://css/blends.png') bottom center;
  background-blend-mode: %s;
  min-width: 200px;
  min-height: 200px;
}

image.blend2 {
  background: url('resource://css/cmy.jpg') top center,
              url('resource://css/cmy.jpg') center center,
              url('resource://css/cmy.jpg') bottom center;
  background-blend-mode: %s;
  min-width: 200px;
  min-height: 200px;
}





代码下载地址:http://download.csdn.net/detail/a87b01c14/9594728

你可能感兴趣的:(PyGObject,PyGobject详解)