英文:
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正是这样的:
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:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论