【发布时间】:2021-02-04 21:40:03
【问题描述】:
我有 4 个 PyGObject 应用程序正在从旧版 PyGObject 更新到 PyGObject 3.38。
其中三个应用程序进展顺利。第四,hcm(https://stromberg.dnsalias.org/~strombrg/hcm/)比较麻烦。该应用程序可以运行,但我收到了一个想要消除的 DeprecationWarning。
警告看起来像:
$ ./hcm.py --gui
./hcm.py:1035: DeprecationWarning: Gtk.Widget.modify_fg is deprecated
gui_stuff.modify_fg(Gtk.StateType.NORMAL, Gdk.color_parse(color))
当前发出警告的代码如下所示:
def color_traversal(gui_stuff, color):
"""Traverse a widget hierarchy, changing colors as we go."""
# I'm thinking the stack requirements won't actually be that hefty, at least as
# long as we're talking about a hierarchy and not a proper graph ^_^
if hasattr(gui_stuff, 'get_children'):
for child in gui_stuff.get_children():
color_traversal(child, color)
if hasattr(gui_stuff, 'modify_fg'):
# modify_fg is deprecated
gui_stuff.modify_fg(Gtk.StateType.NORMAL, Gdk.color_parse(color))
# if hasattr(gui_stuff, 'override_color'):
# override_color is deprecated too
# rgba = Gdk.RGBA()
# rgba.parse(color)
# gui_stuff.override_color(Gtk.StateType.NORMAL, rgba)
# if hasattr(gui_stuff, 'set_rgba'):
# This doesn't appear to work; none of the widgets I'm traversing have a set_rgba method
# gdk_color = Gdk.color_parse(color)
# rgba = Gdk.RGBA(gdk_color.red, gdk_color.green, gdk_color.blue, 1.0)
# gui_stuff.set_rgba(rgba)
# Maybe try get_style_context() ?
# Maybe try CSS: https://shallowsky.com/blog/programming/styling-gtk3-with-css-python.html
"gui_stuff" 只是一个框或按钮之类的东西。 color_traversal 函数只是递归地遍历小部件及其组件小部件,将它们的前景色更改为调用者指定的颜色。
我有很多 Python 经验,但几乎没有 CSS 经验。 CSS是现在做这些事情的唯一方法吗?我必须说,与函数调用相比,学习一门新语言来设置一些小部件的颜色似乎有点要求。
我已经搜索了几个小时如何做到这一点,但只发现了已弃用的函数或 CSS 方法的不完整描述。
谢谢!
【问题讨论】: