【问题标题】:Linux - Synchronising tkinter GUI background and foreground colors with system theme colorsLinux - 将 tkinter GUI 背景和前景色与系统主题颜色同步
【发布时间】:2021-12-21 22:25:42
【问题描述】:

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

例如,我试过:

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

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

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

【问题讨论】:

    标签: python linux windows tkinter background-color


    【解决方案1】:

    这个答案是我在尝试Reference 中描述的说明后发现的。 Ubuntu 20.04 发行版使用 gnome 显示管理器,其外观由以 CSS 格式存储的 gnome-shell 主题定义。要访问它们,必须使用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.
    

    【讨论】:

      【解决方案2】:

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

      Effbot-webarchiv

      【讨论】:

      • 我在 20.04 发行版中找不到 xrgb.txt... Effbot 信息是否已过时?
      • @SunBear 您也许可以找到名为 xrgb.txt 的文件。在 unix 中有很多可用的窗口管理器,并且这些没有标准。作为替代方案,您可以考虑通过小部件获取颜色。
      • @SunBear 你可以试试xcolors 工具。
      • 对于使用 Gnome 的 Ubuntu 20.04 发行版,我必须使用 gresource 命令来提取信息。我在回答中描述了它。
      • @SunBear 很高兴你找到了符合预期的东西。
      猜你喜欢
      • 2012-07-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-13
      • 2010-10-03
      相关资源
      最近更新 更多