2015-09-27 42 views
2

我有一個非常簡單的應用PyGObject設置樣式屬性:在PyGObject

from gi.repository import Gtk, Gdk 


class Window(Gtk.Window): 
    def __init__(self, *args, **kwargs): 
     super().__init__(*args, **kwargs) 
     self.set_border_width(5) 

     self.progress = Gtk.ProgressBar() 
     self.progress.set_fraction(0.5) 

     self.box = Gtk.Box() 
     self.box.pack_start(self.progress, True, True, 0) 

     self.add(self.box) 
     self.connect('delete-event', Gtk.main_quit) 
     self.show_all() 


win = Window() 
Gtk.main() 

Application

我想有進度條厚。所以我發現有一個樣式屬性爲:

Name      Type Default Flags Short Description 
min-horizontal-bar-height int  6  r/w Minimum horizontal height of the progress bar 

但是,我似乎不能以任何方式設置樣式屬性我試過。

1)我試圖使用CSS:

style_provider = Gtk.CssProvider() 

css = b""" 
GtkProgressBar { 
    border-color: #000; 
    min-horizontal-bar-height: 10; 
} 
""" 

style_provider.load_from_data(css) 

Gtk.StyleContext.add_provider_for_screen(
    Gdk.Screen.get_default(), 
    style_provider, 
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION 
) 

但我得到一個錯誤:

GLib.Error: gtk-css-provider-error-quark: <data>:4:37'min-horizontal-bar-height' is not a valid property name (3)

2)我試圖set_style_property方法和在this answer to similar question描述的所有方法。

一個)

self.progress.set_property('min-horizontal-bar-height', 10)

TypeError: object of type 'GtkProgressBar' does not have property 'min-horizontal-bar-height'

B)

self.progress.set_style_property('min-horizontal-bar-height', 10)

AttributeError: 'ProgressBar' object has no attribute 'set_style_property'

c)中

self.progress.min_horizontal_bar_height = 10

GLib.Error: gtk-css-provider-error-quark: <data>:4:37'min-horizontal-bar-height' is not a valid property name (3)

d)

self.progress.props.min_horizontal_bar_height = 10

AttributeError: 'gi._gobject.GProps' object has no attribute 'min_horizontal_bar_height'

E)

self.progress.set_min_horizontal_bar_height(10)

AttributeError: 'ProgressBar' object has no attribute 'set_min_horizontal_bar_height'

任何想法如何獲得更厚的進度條?

回答

5

在CSS中,你必須前綴的樣式屬性以短線的名稱和類的style屬性所屬的名字:

GtkProgressBar { 
    -GtkProgressBar-min-horizontal-bar-height: 10px; 
} 

他們的目的不是在代碼中設置,所以沒有相應的設置方法style_get_property()

+0

太棒了!有沒有一個很好的文檔,通常如何使用CSS在GTK中進行樣式設計?就像例如在運行時單獨的小部件,造型整個應用程序等?我很難找到任何相關的東西。 – Fenikso

+1

[這裏是GTK CSS樣式文檔](https://developer.gnome.org/gtk3/stable/GtkCssProvider。HTML#GtkCssProvider.description)。這可能會更好,但它是一個相當不錯的概述你可以做什麼。 – ptomato

+0

對不起,什麼?這個「類名和短劃線的前綴」到底在哪裏?*真的*記錄在案?無論如何,不​​在鏈接頁面中。 – yPhil