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



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

例如,我尝试过:

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

CCD_ 1呈浅灰色;它与使用中的深色雅鲁主题所使用的黑色不同。

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

在X窗口系统上,颜色名称由X服务器定义。您可能能够找到一个名为xrgb.txt的文件,该文件包含颜色名称和相应RGB值的列表。在Windows和Macintosh系统中,颜色名称表被内置到Tk.

Effbot webarchiv

这个答案是我在尝试本参考资料中描述的说明后发现的。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.

接下来运行以下命令。从命令输出中,识别感兴趣的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.

最新更新