通过Python3在Mac上检测锁定屏幕

编程入门 行业动态 更新时间:2024-10-27 04:28:08
本文介绍了通过Python3在Mac上检测锁定屏幕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

试图找到一种使用Python3检测屏幕是否在Mac系统上锁定的方法

Trying to find a way to detect if the screen is locked on a Mac system, using Python3

我尝试了以下操作,它给出了KeyError:

I've tried the following, and it gives a KeyError:

import Quartz gui_dict = Quartz.CGSessionCopyCurrentDictionary() print(gui_dict['CGSSessionScreenIsLocked'])

import Quartz all_windows = Quartz.CGWindowListCopyWindowInfo(Quartz.kCGWindowListOptionOnScreenOnly, Quartz.kCGNullWindowID) for x in all_windows: if x["kCGWindowOwnerName"] == "loginwindow" print("Locked!")

此代码可在Python2上运行,但考虑到需要使用/usr/bin/python来导入Quartz,因此不适用于此代码.

This code works on Python2, but not 3 considering /usr/bin/python needs to be used to import Quartz.

推荐答案

安装quartz- pip install pyobjc-framework-Quartz

很多简单的代码-

import Quartz d = Quartz.CGSessionCopyCurrentDictionary() print('CGSSessionScreenIsLocked' in d.keys())

更多推荐

通过Python3在Mac上检测锁定屏幕

本文发布于:2023-07-05 15:58:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1039161.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:屏幕   Mac

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!