在使用Glade创建的Python GTK3程序中导入CSS文件 [英] Import CSS file in Python GTK3 program created using Glade

查看:139
本文介绍了在使用Glade创建的Python GTK3程序中导入CSS文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试查找有关添加彩色按钮小部件的信息.我尝试将suggested-action添加为类,但是按钮仍然是灰色的.所以我想用自己的样式信息编写自己的css文件. 我正在使用林间空地来创建林间空地文件,并从其在main.py中构建gui.

I try to find information about adding a colored button widget. I tried adding suggested-action as class, but the button is still grey. So i want to write my own css file with my own style information. I'm using glade to create the glade file and build the gui from that in my main.py.

我必须在哪里将css文件放在源代码树中,以及如何导入它?

Where do I have to put the css file in my source tree and how do I import it?

推荐答案

此处有HowDoI指南: https ://wiki.gnome.org/HowDoI/CustomStyle

There is a HowDoI guide here: https://wiki.gnome.org/HowDoI/CustomStyle

在Python中,您必须先导入Gdk:

In Python you have to first import Gdk:

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

然后您可以在启动时设置CSS:

Then you can set the CSS at startup:

screen = Gdk.Screen.get_default()
provider = Gtk.CssProvider()
provider.load_from_path("/path/to/style.css")
Gtk.StyleContext.add_provider_for_screen(screen, provider, Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION)

在这里,我建议使用绝对路径.如果使用相对路径,如果工作目录 不是项目目录,则会遇到问题.例如,如果您使用相对路径,则将不起作用:

Here I recommend using an absolute path. If you use relative paths you will get into problems if the working directory is not the directory of the project. For example, if you use relative paths, this will not work:

cd somedirectory
python /home/user/project/main.py
# Error: cannot find style.css

注意:当然,背景颜色属性仅在没有 背景图像的情况下有效设置填充背景. Adwaita主题在按钮上设置背景图像.因此,将其删除:

NOTE: of course, the background-color property only works if there is no background-image set filling the background. The Adwaita theme sets the background-image on buttons. So remove it:

button {
  background: none;
  background-color: red;
}

这篇关于在使用Glade创建的Python GTK3程序中导入CSS文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆