Example
Example
Code:
#!/usr/bin/env Python3 #-*-Mode:python; Py-indent-offset:4-*-# vim:tabstop=4 shiftwidth=4 expandtab # Copyright (C) 2013 Gian Mario Tagliaretti <gianmt@g Nome.org> # This library are free software; Can redistribute it and/or # modify it under the terms of the GNU Lesser General public # License as published by the Free Software Foundation;
Either # version 2.1 of the License, or (at your option) any later version. # This library is distributed in the hope that it'll be useful, # but without any WARRANTY; Without even the implied warranty of # merchantability or FITNESS for A particular purpose.
The GNU # Lesser general public License for more details. # You are should have received a copy of the GNU Lesser General public # License along and this library; If not, write to the free Software # Foundation, INC, Wuyi Franklin St, Fifth Floor, Boston, MA 02110-1301 # USA TITLE = "CSS theming/multiple Backgrounds" "DESCRIPTION =" "GTK themes are writTen using CSS.
The Every widget is build of multiple items so you can style very similarly to a regular website. "" "Import gi gi.require_version (' Gtk ', ' 3.0 ') from gi.repository import Gtk, GdK, Pango, Gio, GLib class CSSMULTIPLEBG Sapp:def __init__ (self): #: Store The last successful parsing of the CSS so we can revert #:
Case is an error. Self.last_good_text = ' #: Set when we receive a Parsing-error callback. This is needed #: to handle logic after a Parsing-error callback which does no raise #: an exception with Provider.load_from_data () Self.last_error_code = 0 Self.window = Gtk.window () self.window.set_ti Tle (' CSS multiplebgs ') self.window.set_default_size (+) Self.window.set_border_width sel F.window.connect (' Destroy ', Lambda w:gtk.main_quit ()) overlay = Gtk.overlay () overlay.add_events (gdk.eve
Ntmask.enter_notify_mask | Gdk.EventMask.LEAVE_NOTIFY_MASK | Gdk.EventMask.POINTER_MOTION_MASK) Self.infobar = Gtk.infobar () Self.infolabel = Gtk.label () Self . Infobar.get_content_area (). Pack_start (Self.infolabel, False, False, 0) Self.infobar.set_message_type (gtk.messaget Ype. WARNING) box = Gtk.box (orientation=gtk.orientation.vertical) box.pack_start (overlay, expand=true, FILL=TR
UE, padding=0) Box.pack_start (Self.infobar, Expand=false, Fill=true, padding=0) self.window.add (box)
Canvas = Gtk.drawingarea () canvas.set_name ("Canvas") canvas.connect ("Draw", Self.drawing_area_draw)
Overlay.add (canvas) button = Gtk.button () button.add_events (Gdk.EventMask.ENTER_NOTIFY_MASK |
Gdk.EventMask.LEAVE_NOTIFY_MASK | Gdk.EventMask.POINTER_MOTION_MASK) button.set_name ("Bricks-button") button.set_halign (Gtk.Align.CENTER) button.set_valign (Gtk.Align.CENTER) button.set_size_request overlay.add_overlay (button ) paned = gtk.paned (orientation=gtk.orientation.vertical) Overlay.add_overlay (paned) # We need a Filler so we get a handle box = Gtk.box (orientation=gtk.orientation.vertical) paned.add (box) buf fer = Gtk.textbuffer () Buffer.create_tag (tag_name= "Warning", Underline=pango.underline.single) Buffer.crea Te_tag (tag_name= "error", underline=pango.underline.error) Provider = Gtk.cssprovider () buffer.connect ("C
Hanged ", self.css_text_changed, provider) provider.connect (" Parsing-error ", Self.show_parsing_error, buffer) TextView = Gtk.textview () textview.set_buffer (buffer) scrolled = Gtk.scrolledwindow () Scrolle
D.add (TextView) paned.add (scrolled) bytes = Gio.resources_lookup_data ("/css/css_multiplebgs.css", 0) Buffer.set_text (ByteS.get_data (). Decode (' Utf-8 ')) Self.apply_css (Self.window, provider) Self.window.show_all () self.i
Nfobar.hide () @staticmethod def drawing_area_draw (widget, cairo_t): Context = Widget.get_style_context ()
Gtk.render_background (context, cairo_t, 0, 0, widget.get_allocated_width (),
Widget.get_allocated_height ()) gtk.render_frame (context, cairo_t, 0, 0, Widget.get_allocated_width (), Widget.get_allocated_height ()) def apply_css (self, Widge T, provider): Gtk.StyleContext.add_provider (Widget.get_style_context (), Prov Ider, Gtk.style_provider_priority_application) if Isinstance (widget, gtk.co Ntainer): Widget.forall (Self.apply_css, provider) def show_parsing_error (self, provider, section, error, Buffer): Start= Buffer.get_iter_at_line_index (Section.get_start_line (), Section.get_start_
Position ()) end = Buffer.get_iter_at_line_index (Section.get_end_line (),
Section.get_end_position ()) if Error.code = = Gtk.CssProviderError.DEPRECATED:tag_name = "Warning" Else:tag_name = "error" Self.last_error_code = Error.code Self.infolabel.set_text (E Rror.message) Self.infobar.show_all () buffer.apply_tag_by_name (tag_name, start, end) def css_text_ch Anged (self, buffer, provider): start = Buffer.get_start_iter () end = Buffer.get_end_iter () buffer. Remove_all_tags (start, end) Text = Buffer.get_text (start, End, False). Encode (' Utf-8 ') # Ignore CSS errors
As they are shown by highlighting Try:provider.load_from_data (text) except Glib.gerror as E:
Print (e) If E.domain!= ' Gtk-css-provider-error-quark ': Raise e # If the Parsing-error callback is ever run (even in the case of warnings) # load the last good CSS text, ran without any warnings. Otherwise # We may have a discrepancy in ' last_good_text ' vs the current buffer # causing Section.get_star
T_position () to give back a invalid position # for the editor buffer.
If Self.last_error_code:provider.load_from_data (self.last_good_text) self.last_error_code = 0 Else:self.last_good_text = text Self.infobar.hide () Gtk.StyleContext.reset_widgets (
Gdk.Screen.get_default ()) def main (): Cssmultiplebgsapp () Gtk.main () if __name__ = = ' __main__ ': Import os 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 ()
Css_multiplebgs.css
/* You can edit the "* appearance of this window." * Is careful, if you are screw it up and nothing might is visible * anymore. :) * * * CSS resets all properties to their defaults values * and overrides all user settings and the theme in U
SE */@import URL ("Resource://css/css_reset.css");
@import url ("Resource://css/css_view.css");
#canvas {transition-property:background-color, background-image;
transition-duration:0.5s;
Background-color: #4870bc; }/* The gradients below are adapted versions of Lea Verou ' s CSS3 patterns, * licensed under the MIT license: * Copyrig
HT (c) Lea Verou, http://lea.verou.me/* * * Https://github.com/LeaVerou/CSS3-Patterns-Gallery//**********
* Bricks * **********/#canvas {background-color: #999; Background-image:linear-gradient (205deg, #b42, #b42 23px, Transparent 23px), Linear-gradient (25deg, #d42, #d42 23px, Transparent 23px), Linear-gradient (205deg, #b42, #b42 23px, Transparent 23px), Linear-gradient (25deg, #d42, #d42 23px, transparent 23px);
BACKGROUND-SIZE:58PX 58px;
background-position:0px 6px, 4px 31px, 29px 35px, 34px 2px;
} #canvas: Backdrop {background-color: #444; Background-image:linear-gradient (205deg, #999, #999 23px, Transparent 23px), Linear-gradient (25deg, #888, #888 23px, Transparent 23px), Linear-gradient (205deg, #999, #999 23px, Transparent 23px), Linear-gradient (25deg, #888, #888 2
3px, transparent 23px);
BACKGROUND-SIZE:58PX 58px;
background-position:0px 6px, 4px 31px, 29px 35px, 34px 2px;
} #bricks-button {background-color: #eef;
Background-image:-gtk-scaled (URL (' resource://css/brick.png '), url (' resource://css/brick2.png '));
Background-repeat:no-repeat;
Background-position:center;
}/********** * Tartan * **********/#canvas {background-color: #662e2c; Background-image:repeating-linear-gradient (transparent, transparent 50px, Rgba (0, 0, 0,. 4) 50px, Rgba (0, 0, 0,. 4) 53px, transparent 53px, transparent 63px, Rgba (0, 0, 0,. 4) 63px, Rgba (0, 0, 0,. 4) 66px, T Ransparent 66px, Transparent 116px, Rgba (0, 0, 0,. 5) 116px, Rgba (0, 0, 0,. 5) 166px, RGBA (255, 255, 255,. 2) 166p X, Rgba (255, 255, 255,. 2) 169px, Rgba (0, 0, 0,. 5) 169px, Rgba (0, 0, 0,. 5) 179px, RGBA (255, 255, 255,. 2) 179px, RGB A (255, 255, 255,. 2) 182px, Rgba (0, 0, 0,. 5) 182px, Rgba (0, 0, 0,. 5) 232px, transparent 232px), repeating-linear -gradient (90deg, transparent, transparent 50px, Rgba (0, 0, 0,. 4) 50px, Rgba (0, 0, 0,. 4) 53px, transparent 53 PX, transparent 63px, Rgba (0, 0, 0,. 4) 63px, Rgba (0, 0, 0,. 4) 66px, transparent 66px, transparent 116px, RGB A (0, 0, 0,. 5) 116px, Rgba (0, 0, 0,. 5) 166px, RGBA (255, 255, 255,. 2) 166px, RGBA (255, 255, 255,. 2) 169px, R
GBA (0, 0, 0,. 5) 169px, Rgba (0, 0, 0,. 5) 179px, RGBA (255, 255, 255,. 2) 179px, RGBA (255, 255, 255,. 2) 182px, RGBA (0, 0,0,. 5) 182px, Rgba (0, 0, 0,. 5) 232px, transparent 232px), repeating-linear-gradient ( -55deg, transparent, transparent
1px, Rgba (0, 0, 0,. 2) 1px, Rgba (0, 0, 0,. 2) 4px, transparent 4px, transparent 19px, Rgba (0, 0, 0, 2) 19px, RGBA (0, 0, 0,. 2) 24px, transparent 24px, transparent 51px, Rgba (0, 0, 0,. 2) 51px, Rgba (0, 0, 0,
.2) 54px, transparent 54px, transparent 74px);
} #canvas: Backdrop {background-color: #333;}
/*********** * Stripes * ***********/#canvas {background-color: #4870bc; Background-image:linear-gradient (to left, transparent, RGBA (255, 255, 255,.) 50%, Transparent 50%), Linear-gradien T (to left, transparent, RGBA (255, 255, 255,.) 50%, Transparent 50%), linear-gradient (to left, transparent, transpar Ent 50%, RGBA (255, 255, 255,.) 50%), Linear-gradient (to left, transparent, transparent 50%, RGBA (255, 255, 255,. 19)
) 50%);
BACKGROUND-SIZE:29PX, 59px, 73px, 109px; } #canvas: Backdrop {BACkground-color: #555;
}/*************** * Lined Paper * ***************/#canvas {background-color: #fff; Background-image:linear-gradient (90deg, Transparent 79px, alpha (#f98195, 0.40) 79px, #f98195 80px, Alpha (#f98195, 0.40) 81px, Transparent 81px)