Linux - 将 tkinter GUI 背景和前景色与系统主题颜色同步

Linux - Synchronising tkinter GUI background and foreground colors with system theme colors

我喜欢创建一个背景色和前景色与系统主题背景色和前景色相似的 tkinter GUI。我如何通过 python 和 tkinter 做到这一点?

例如,我试过:

>>> root = tk.Tk()
>>> color = root.cget('background')
>>> print(f'color={color}')`
>>> color=#d9d9d9

#d9d9d9 呈浅灰色;它与正在使用的深色 Yaru 主题使用的黑色不同。

一般来说,在创建 tkinter GUI 时,我希望 GUI 能够发现系统背景和前景色。我应该如何通过 python 和 tkinter 做到这一点?

On an X window system, the color names are defined by the X server. You might be able to locate a file named xrgb.txt which contains a list of color names and the corresponding RGB values. On Windows and Macintosh systems, the color name table is built into Tk.

Effbot-webarchiv

这个答案是我在尝试 Reference 中描述的说明后发现的。 Ubuntu 20.04 发行版使用 gnome 显示管理器,其外观由 gnome-shell 主题定义,并以 CSS 格式存储。要访问它们,必须使用 gresource 命令来提取它。

步骤:

首先,复制/usr/share/gnome-shell/theme/Yaru/gnome-shell-theme.gresource,例如:

$ cp /usr/share/gnome-shell/theme/Yaru/gnome-shell-theme.gresource ~/tmp
$ cd ~/tmp.

Next 运行 下面的命令。从命令输出中,确定感兴趣的 CSS 文件。在我的例子中,它是 /org/gnome/shell/theme/Yaru-dark/gnome-shell.css.

$ gresource list gnome-shell-theme.gresource
/org/gnome/shell/theme/Yaru-dark/gnome-shell-high-contrast.css
/org/gnome/shell/theme/Yaru-dark/gnome-shell.css
/org/gnome/shell/theme/Yaru/gnome-shell-high-contrast.css
/org/gnome/shell/theme/Yaru/gnome-shell.css
/org/gnome/shell/theme/calendar-today.svg
/org/gnome/shell/theme/checkbox-dark.svg
/org/gnome/shell/theme/checkbox-focused-dark.svg
/org/gnome/shell/theme/checkbox-focused.svg
/org/gnome/shell/theme/checkbox-off-dark.svg
/org/gnome/shell/theme/checkbox-off-focused-dark.svg
/org/gnome/shell/theme/checkbox-off-focused.svg
/org/gnome/shell/theme/checkbox-off.svg
/org/gnome/shell/theme/checkbox.svg
/org/gnome/shell/theme/dash-placeholder.svg
/org/gnome/shell/theme/dmb_trees.jpg
/org/gnome/shell/theme/gdm3.css
/org/gnome/shell/theme/icons/scalable/actions/pointer-double-click-symbolic.svg
/org/gnome/shell/theme/icons/scalable/actions/pointer-drag-symbolic.svg
/org/gnome/shell/theme/icons/scalable/actions/pointer-primary-click-symbolic.svg
/org/gnome/shell/theme/icons/scalable/actions/pointer-secondary-click-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/eye-not-looking-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/eye-open-negative-filled-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-caps-lock-filled-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-enter-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-hide-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-layout-filled-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/keyboard-shift-filled-symbolic.svg
/org/gnome/shell/theme/icons/scalable/status/message-indicator-symbolic.svg
/org/gnome/shell/theme/no-events.svg
/org/gnome/shell/theme/no-notifications.svg
/org/gnome/shell/theme/pad-osd.css
/org/gnome/shell/theme/process-working.svg
/org/gnome/shell/theme/toggle-off-dark.svg
/org/gnome/shell/theme/toggle-off-hc.svg
/org/gnome/shell/theme/toggle-off.svg
/org/gnome/shell/theme/toggle-on-dark.svg
/org/gnome/shell/theme/toggle-on-hc.svg
/org/gnome/shell/theme/toggle-on.svg

要提取那个 CSS 文件,运行 这个命令:

gresource extract gnome-shell-theme.gresource /org/gnome/shell/theme/Yaru-dark/gnome-shell.css > output_yaru_dark.css

使用任何文本编辑器,我能够查看 output_yaru_dark.css 以找到全局背景颜色定义于:

/* Global Values */
stage {
  font-size: 11pt;
  color: #3D3D3D; } <--- global background colour for the Yaru theme.