根据
this SO post
您可以使用以下命令检查MacOS上连接的监视器的规格:
$ system_profiler SPDisplaysDataType
这将返回以下信息:
Graphics/Displays:
NVIDIA GeForce GT 640M:
Chipset Model: NVIDIA GeForce GT 640M
Type: GPU
Bus: PCIe
PCIe Lane Width: x16
VRAM (Total): 512 MB
Vendor: NVIDIA (0x10de)
Device ID: 0x0fd8
Revision ID: 0x00a2
ROM Revision: 3707
Displays:
iMac:
Display Type: LCD
Resolution: 1920 x 1080
Pixel Depth: 32-Bit Color (ARGB8888)
Main Display: Yes
Mirror: Off
Online: Yes
Built-In: Yes
Connection Type: DisplayPort
我的假设是,当没有连接任何显示时,将没有
Display Type
,
Resolution
或者其他人。
Here
有关使用命令行参数在python中运行命令行程序并接收输出(即:
>>> import subprocess
>>> result = subprocess.run(['system_profiler', 'SPDisplaysDataType'], stdout=subprocess.PIPE)
>>> result.stdout
和result.stdout将包含返回的字符串(如前所示)。然后,您可以搜索返回的字符串,您可能需要这些字符串的任何参数:
if "Display Type" not in result.stdout:
screenConnected = false
playMusic()
显然,它值得检查其他参数并添加一些错误处理,但也许这是一个开始的地方。
我不确定是否有回调方法/我自己也没有找到任何Python库,但是请在我的答案中评论任何一个,我将更新。
让我知道这是否有效!