2015-09-27 46 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