簡體   English   中英

GTK3重用CSS狀態背景顏色

[英]GTK3 reuse CSS state background colors

我正在移植PyGTK2應用程序。

這個應用程序做這樣的事情:

        self.normal_color = editor.style.base [Gtk.STATE_NORMAL]
        self.insensitive_color = editor.style.base [Gtk.STATE_INSENSITIVE]

然后設置它:

        if editable: self.editor.modify_base (Gtk.STATE_NORMAL, self.normal_color)
        else:        self.editor.modify_base (Gtk.STATE_NORMAL, self.insensitive_color)

self.editor是一個GtkTextView 之所以不將它設置為editor.set_sensitive(False)是因為編輯器中的用戶程序和加載代碼時編輯器仍然可以選擇,我們不希望文本變灰。

這是我使用CSS的端口的MWE:

import gi
gi.require_version('Gtk', '3.0')
gi.require_version('Gdk', '3.0')
from gi.repository import Gtk, Gdk

view = Gtk.TextView()
view.set_name('editable')
scroll = Gtk.ScrolledWindow()
scroll.add(view)

def button_clicked(button):
    edit = not view.get_editable()
    view.set_name('editable' if edit else 'readonly')
    button.set_label('Load' if edit else 'Edit')
    view.set_editable(edit)

button = Gtk.Button('Load')
button.connect('clicked', button_clicked)

box = Gtk.Box.new(Gtk.Orientation.VERTICAL, 0)
box.pack_start(scroll, True, True, 0)
box.pack_start(button, False, True, 0)

window = Gtk.Window()
window.set_default_size(300, 300)
window.add(box)
window.show_all()
window.connect('delete_event', Gtk.main_quit)

css = Gtk.CssProvider()
css.load_from_path('test.css')
style = Gtk.StyleContext()
style.add_provider_for_screen(Gdk.Screen.get_default(), css,
    Gtk.STYLE_PROVIDER_PRIORITY_USER)

Gtk.main()

和各自的CSS文件:

#editable {
    background-color: white;
    /*background-color: @normal_bg_color;*/
}

#readonly {
    background-color: @insensitive_bg_color;
}

也就是說,我使用CSS名稱來動態更改背景顏色。 我沒有找到一個更簡單的編程方式,因為一切似乎都被棄用了。

我的問題是,無論出於何種原因, @normal_bg_color對於GtkTextView @normal_bg_color是白色的,這是奇怪的。

我猜@*_bg_color是通用顏色,並不是特定於小部件。 還是我錯了?

我想要一些@GtkTextView:normal_bg_color ...

view.get_style_context().get_background_color()已被棄用。 是否有可能獲得用戶的主題顏色?

由於歷史原因,您正在尋找的顏色可能是@base_color GTK一直區分“基礎”顏色(用於文本輸入小部件)和“背景”顏色(用於其他所有顏色)。

這不是文檔,但是此頁面上提供了示例。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM