例子
代码:
#!/usr/bin/env python3
# Created by xiaosanyu at 16/7/18
# section 156
#
# author: xiaosanyu
# website: yuxiaosan.tk \
# http://blog.csdn.net/a87b01c14
# created: 16/7/18
TITLE = "Shadows"
DESCRIPTION = """
This demo shows how to use CSS shadows.
"""
import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, Gdk, Gio, GLib, Pango
import os
class ShadowsWindow(Gtk.Window):
def __init__(self):
Gtk.Window.__init__(self, title="Shadows")
self.set_default_size(400, 300)
paned = Gtk.VPaned()
self.add(paned)
child = self.create_toolbar()
paned.add(child)
text = Gtk.TextBuffer()
text.create_tag(tag_name="warning", underline=Pango.Underline.SINGLE)
text.create_tag(tag_name="error", underline=Pango.Underline.ERROR)
provider = Gtk.CssProvider()
container = Gtk.ScrolledWindow()
paned.add(container)
child = Gtk.TextView.new_with_buffer(text)
container.add(child)
text.connect("changed", self.css_text_changed, provider)
bytes = Gio.resources_lookup_data("/css/gtk.css", 0)
text.set_text(bytes.get_data().decode('utf-8'))
provider.connect("parsing-error", self.show_parsing_error, child.get_buffer())
self.apply_css(self, provider)
@staticmethod
def create_toolbar():
toolbar = Gtk.Toolbar()
toolbar.set_valign(Gtk.Align.CENTER)
item = Gtk.ToolButton()
item.set_icon_name("go-next")
toolbar.insert(item, -1)
item = Gtk.ToolButton()
item.set_icon_name("go-previous")
toolbar.insert(item, -1)
item = Gtk.ToolButton(label="Hello World")
item.set_is_important(True)
toolbar.insert(item, -1)
return toolbar
@staticmethod
def css_text_changed(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
Gtk.StyleContext.reset_widgets(Gdk.Screen.get_default())
@staticmethod
def show_parsing_error(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"
buffer.apply_tag_by_name(tag_name, start, end)
def apply_css(self, widget, provider):
Gtk.StyleContext.add_provider(widget.get_style_context(),
provider,
Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)
if isinstance(widget, Gtk.Container):
widget.forall(self.apply_css, provider)
def main():
win = ShadowsWindow()
win.connect("delete-event", Gtk.main_quit)
win.show_all()
Gtk.main()
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()
gtk.css
/* You can edit the text in this window to change the
* appearance of this Window.
* Be careful, if you screw it up, nothing might be visible
* anymore. :)
*/
/* This CSS resets all properties to their defaults values
* and overrides all user settings and the theme in use */
@import url("resource://css/css_reset.css");
@import url("resource://css/css_view.css");
/* Get a nice background for the window */
.background {
background: linear-gradient(to right, violet, indigo, blue, green, yellow, orange, red);
}
button {
font-size: 20px;
color: goldenrod;
padding: 10px;
border-radius: 5px;
transition: all 250ms ease-in;
border: 1px transparent solid;
}
button:hover {
text-shadow: 3px 3px cyan;
-gtk-icon-shadow: 3px 3px 5px alpha(black, 0.75);
box-shadow: 0px 3px 10px 5px red inset;
border: solid 2px alpha(black, 0.75);
}
button:active {
padding: 11px 9px 9px 11px;
text-shadow: 1px 1px 2.5px alpha(black, 0.6);
-gtk-icon-shadow: 1px 1px 2.5px alpha(black, 0.6);
}