Python代码检测osx中的暗模式El Capitan更改状态栏菜单图标

2024-07-08 12:22:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有客观C代码检测暗模式,以改变状态栏:

[[NSDistributedNotificationCenter defaultCenter] addObserver:self selector:@selector(darkModeChanged:) name:@"AppleInterfaceThemeChangedNotification" object:nil];

类似地,我们如何在python中做同样的事情呢?在


Tags: 代码nameselfobject模式事情selectornil
3条回答

在python中,os模块可以方便地检测模式。在

基本上,我们使用python访问并运行terminal命令来查找默认设置中的AppleInterfaceStyle属性。在

import os

has_interface = os.popen("defaults find AppleInterfaceStyle").read()
if not has_interface:
    print("Use a light Style")
else:
    interface_system = os.popen("defaults read -g AppleInterfaceStyle").read()
    print("Interface Style:" + interface_system) # interface_system = 'Dark\n'

我不知道您是否可以从python中直接执行此操作。但至少可以调用终端命令defaults read -g AppleInterfaceStyle。在

目前它的行为是这样的:如果它的退出代码是0,它报告“暗模式”。如果为1(错误),则可以假定为灯光模式。在我看来,这不是很干净,但它是有效的,并且在Java program中成功地使用了它。在

如何在python中生成一个新进程是另一个问题,这个问题已经是answered。在

在您想检测模式(暗模式或光模式)的任何地方尝试以下几行。在

center = NSDistributedNotificationCenter.defaultCenter()
center.addObserver_selector_name_object_(self,"enableDarkMode",'AppleInterfaceThemeChangedNotification',None)

相关问题 更多 >

    热门问题