在 Python 中检测 OS 暗模式 [英] Detect OS dark mode in Python

查看:36
本文介绍了在 Python 中检测 OS 暗模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用 python 编写一个带有 GUI 的小程序,我正在使用 tkinter.我想做的是为我的程序添加暗模式支持.Mac OS、Ubuntu(至少是 Gnome)和 Windows 10 都有一个系统范围的黑暗模式"设置.这将使所有程序自动以深色主题运行.

I'm writing a small program in python with a GUI that I'm using tkinter for. What I'd like to do is add dark mode support to my program. Mac OS, Ubuntu (at least Gnome) and Windows 10 all have a system-wide setting for "dark mode" that'll make all programs automatically run with a dark theme.

但是我如何检查该设置(理想情况下与操作系统无关),以便我的程序知道它是否需要渲染亮模式或暗模式?我发现了一堆像 darkdetect 这样的库可以为 MacOS 处理这个问题,但我还没有找到适用于 Ubuntu 和 Windows 10 的任何东西.

But how do I check that settings (ideally, OS-independant) so my program knows if it needs to render light mode or dark mode? I found a bunch of libraries like darkdetect that'll handle this for MacOS, but I haven't found anything for Ubuntu and Windows 10.

我知道我可以使用 ttkthemes 之类的东西为我的程序创建一个黑暗主题的设计,但我怎么知道何时启用它?在 Windows 10 或 Ubuntu 20.04 上运行的 python 脚本如何确定用户是否在操作系统设置中启用了暗模式?

I know that I can use stuff like ttkthemes to create a dark themed design for my program, but how do I know when to enable that? How can a python script running on Windows 10 or on Ubuntu 20.04 figure out if the user enabled the dark mode in the operating system settings?

理想情况下,我正在寻找适用于所有三种操作系统的解决方案/代码,但如果这不可能(正如我怀疑的那样),依赖于操作系统的代码也可以.我在任何地方都找不到适用于非 MacOS 系统的正确示例.

Ideally I'm looking for a solution / code that'll work on all three operating systems, but if that's not possible (as I suspect) OS-dependant code would be fine as well. I just can't find proper examples for non-MacOS systems anywhere.

推荐答案

在 macOS 上,Dark/Light 模式出现了,可以通过运行一个简单的命令 defaults read -g AppleInterfaceStyle on 来检查终端.

On macOS, the Dark/Light mode comes under appearance which can be checked by running a simple command defaults read -g AppleInterfaceStyle on the terminal.

  • 在暗模式下,命令返回

  • On Dark mode the command returns

Dark

它只会返回 Dark 意味着 AppleInterfaceStyle 存在.

It'll just return Dark meaning AppleInterfaceStyle exist.

在 Light 模式下返回相同的命令

On Light mode the same command returns

2020-12-18 17:44:21.870 defaults[20253:5665627] 
The domain/default pair of (kCFPreferencesAnyApplication, AppleInterfaceStyle) does not exist

默认情况下启用灯光模式,因此 AppleInterfaceStyle 不存在,这就是它返回错误的原因.

By default light mode is enabled so AppleInterfaceStyle doesn't exist and that's why it is returning an error.

我们可以简单地创建一个函数,如果启用了深色模式,它将返回 True,如果启用了浅色模式,则返回 False.

We can simply make a function which will return True if the Dark mode is enabled and False if the Light mode is enabled.

import subprocess

def check_appearance():
    """Checks DARK/LIGHT mode of macos."""
    cmd = 'defaults read -g AppleInterfaceStyle'
    p = subprocess.Popen(cmd, stdout=subprocess.PIPE,
                         stderr=subprocess.PIPE, shell=True)
    return bool(p.communicate()[0])

这篇关于在 Python 中检测 OS 暗模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆