CSS styling for Gtk Widgets not working (Python, Gtk4)?

huangapple go评论63阅读模式
英文:

CSS styling for Gtk Widgets not working (Python, Gtk4)?

问题

以下是您要翻译的部分:

"In my app I want some kind of profile picture a user can have. At first wanted to use libadwaita's Avatar widget, but it cannot load images from a file, which is a feature that I need. So I thought I would just implement a normal Gtk.Image and set its border-radius to 50%. However, GTK seems to ignore my CSS rules. The image is displayed but without the border radius.
Here is my code:

# in my class MainWindow(Adw.ApplicationWindow)
css_provider = Gtk.CssProvider()
css_provider.load_from_path("style.css")
Gtk.StyleContext.add_provider_for_display(
    Gdk.Display.get_default(),
    css_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)

img = Gtk.Image.new_from_file("some_image.jpg")
img.set_pixel_size(100)

self.some_box.append(img)

code of my style.css:

image { /* I also tried 'GtkImage' and a custom class */
    border: 4px solid #000;
}

EDIT: Solved it using a Gtk.Frame as a wrapper. Set the image as its child."

英文:

In my app I want some kind of profile picture a user can have. At first wanted to use libadwaita's Avatar widget, but it cannot load images from a file, which is a feature that I need. So I thought I would just implement a normal Gtk.Image and set its border-radius to 50%. However, GTK seems to ignore my CSS rules. The image is displayed but without the border radius.
Here is my code:

# in my class MainWindow(Adw.ApplicationWindow)
css_provider = Gtk.CssProvider()
css_provider.load_from_path("style.css")
Gtk.StyleContext.add_provider_for_display(
    Gdk.Display.get_default(),
    css_provider,
    Gtk.STYLE_PROVIDER_PRIORITY_APPLICATION
)

img = Gtk.Image.new_from_file("some_image.jpg")
img.set_pixel_size(100)

self.some_box.append(img)

code of my style.css:

image { /* I also tried 'GtkImage' and a custom class */
    border: 4px solid #000;
}

EDIT: Solved it using a Gtk.Frame as a wrapper. Set the image as its child.

答案1

得分: 0

我刚在ruby-gtk中测试了这个,这里的图片是结果:

CSS styling for Gtk Widgets not working (Python, Gtk4)?

我使用的CSS正是这样的:

image {
  border: 50px solid black;
  border-radius: 50%;
}

这至少应该显示一个圆。

您是否应用了这个CSS?GtkImage已被弃用。

如果您稍微调整一下您的代码,以便其他人可以快速复制/粘贴它,这可能也有所帮助。对我来说,将它放入我用于ruby-gtk3的自动生成中比为python-gtk添加缺失的部分要容易一些。

编辑:实际上,我刚刚注意到您提到了gtk4。gtk4有点奇怪。

在gtk3上,它确实有效。

英文:

I just tested this in ruby-gtk, the image here is the result:

CSS styling for Gtk Widgets not working (Python, Gtk4)?

The CSS I was using is exactly this:

image {
  border: 50px solid black;
  border-radius: 50%;
}

And that should at the least show a circle.

Do you apply the CSS? GtkImage is deprecated.

It may also help if you adapt your code a little bit so others can
quickly copy/paste it. For me it was easier to just put it into an
autogenerate I use for ruby-gtk3, rather than put in the missing
pieces for python-gtk.

Edit: actually I just noticed you refer to gtk4. gtk4 is odd.

On gtk3 it works though.

huangapple
  • 本文由 发表于 2023年2月26日 21:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/75572406.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定