【发布时间】:2019-09-15 19:15:36
【问题描述】:
在使用 Java 编程几年后,我现在正在学习 Vala。
在网上进行了一些广泛的搜索后,我发现了一种使用 CSS 样式表设置 GTK 窗口背景样式的方法,来自给定的示例 here ,它使用了 GTk.Window 类扩展。该代码在我的机器(Ubuntu 19.04)上编译良好,并且小部件的样式符合预期。
我试图将这种方法与 site 中的一种方法结合起来。这里,vala 类扩展到 Gtk.Application,而不是 Gtk.Window。
代码编译并打开窗口,但小部件没有根据样式表设置样式。
public class StyleApp1 : Gtk.Application {
public StyleApp1 () {
Object (
application_id: "com.css.test",
flags: ApplicationFlags.FLAGS_NONE
);
}
protected override void activate () {
var window = new Gtk.ApplicationWindow (this);
window.set_default_size (350, 500);
window.title = "Hello World";
window.get_style_context().add_class("my_window");
var screen = window.get_screen ();
var css_provider = new Gtk.CssProvider();
string path = "styleapp1.css";
// test if the css file exist
if (FileUtils.test (path, FileTest.EXISTS))
{
try {
stdout.printf("File is there");
css_provider.load_from_path(path);
Gtk.StyleContext.add_provider_for_screen(screen, css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER);
} catch (Error e) {
error ("Cannot load CSS stylesheet: %s", e.message);
}
}
var box = new Gtk.Box (Gtk.Orientation.VERTICAL, 10);
window.add (box);
var label = new Gtk.Label ("Thank you");
box.add (label);
var label2 = new Gtk.Label ("Stackoverflow");
label2.get_style_context().add_class("my_class");
box.add (label2);
window.show_all ();
}
public static int main (string[] args) {
var app = new StyleApp1 ();
return app.run (args);
}
}
CSS 文件 (syleapp1.css)
GtkWindow {
font-size: 17px;
}
.my_class {
color: red;
}
.my_window {
background-color: rgba (200, 100, 100, 0.9);
}
介子构建文件:
project('com.css.test' , 'vala' , 'c')
executable (
meson.project_name(),
'StyleApp1.vala',
dependencies: [
dependency('gtk+-3.0')
],
install: true
)
我不知道我错过了什么。有人可以解释并指出我正确的方向吗?
非常感谢提前。
【问题讨论】: