PyGobject(一百零四)CSS系列——在TextView中顯示樣式,可修改

來源:互聯網
上載者:User

例子

例子

代碼:

#!/usr/bin/env python3# section 154# -*- Mode: Python; py-indent-offset: 4 -*-# vim: tabstop=4 shiftwidth=4 expandtab## Copyright (C) 2013 Gian Mario Tagliaretti <gianmt@gnome.org>## This library is free software; you 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 will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU# Lesser General Public License for more details.## You should have received a copy of the GNU Lesser General Public# License along with this library; if not, write to the Free Software# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301# USATITLE = "CSS Basics"DESCRIPTION = """Gtk themes are written using CSS. Every widget is build of multiple itemsthat you can style very similarly to a regular website."""import osimport gigi.require_version('Gtk', '3.0')from gi.repository import Gtk, Gdk, Pango, Gio, GLibclass CSSBasicsApp:    def __init__(self):        #: Store the last successful parsing of the css so we can revert        #: this in case of 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 not raise        #: an exception with provider.load_from_data()        self.last_error_code = 0        self.window = Gtk.Window()        self.window.set_title('CSS Basics')        self.window.set_default_size(400, 300)        self.window.set_border_width(10)        self.window.connect('destroy', lambda w: Gtk.main_quit())        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.MessageType.WARNING)        scrolled = Gtk.ScrolledWindow()        box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL)        box.pack_start(scrolled, expand=True, fill=True, padding=0)        box.pack_start(self.infobar, expand=False, fill=True, padding=0)        self.window.add(box)        provider = Gtk.CssProvider()        buffer = Gtk.TextBuffer()        buffer.create_tag(tag_name="warning", underline=Pango.Underline.SINGLE)        buffer.create_tag(tag_name="error", underline=Pango.Underline.ERROR)        buffer.connect("changed", self.css_text_changed, provider)        provider.connect("parsing-error", self.show_parsing_error, buffer)        textview = Gtk.TextView()        textview.set_buffer(buffer)        scrolled.add(textview)        bytes = Gio.resources_lookup_data("/css/css_basics.css", 0)        buffer.set_text(bytes.get_data().decode('utf-8'))        self.apply_css(self.window, provider)        self.window.show_all()        self.infobar.hide()    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 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(error.message)        self.infobar.show_all()        buffer.apply_tag_by_name(tag_name, start, end)    def css_text_changed(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:            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 that ran without any warnings. Otherwise        # we may have a discrepancy in "last_good_text" vs the current buffer        # causing section.get_start_position() to give back an 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():    CSSBasicsApp()    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()

css_basics.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");/*設定預設的顏色,字型和邊框*/* {    color: green;    font-family: Monospace;    border: 1px solid;}/* 設定選中文本的顏色和背景顏色 */selection {    background-color: darkGreen;    color: green;}

代碼解析:
跟上一個例子有所不同的是,這個代碼使用一個TextView來顯示css檔案,並監聽常值內容的改變,當常值內容改變時,使用
provider.load_from_data(text)重新載入CSS樣式,當載入錯誤出現錯誤就會調用show_parsing_error()方法顯示錯誤資訊

provider.connect("parsing-error", self.show_parsing_error, buffer)





代碼下載地址:http://download.csdn.net/detail/a87b01c14/9594728

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.