如何从Python线程继续运行PowerShell命令?

2024-05-15 08:57:57 发布

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

我想得到我的屏幕亮度,并有一个tkinter规模设置亮度自己。我需要PowerShell命令获取屏幕亮度和设置它。我已经知道如何做到这一点,但我假设线程导入PowerShell(像cmd一样启动它,需要一段时间),然后运行命令,关闭PowerShell并再次打开它。这个方法很慢,我可以从TaskManager看到,这个进程占用大约30%的CPU性能,而不是通常占用的几个百分点。你知道吗

这就是我如何得到亮度和设置亮度

import os

while True:
    os.system("powershell Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness >brightness.txt")
    brightness_file = open("brightness.txt", "r")
    #This is the brightness value 0-100 as string
    brightness_now = brightness_file.read().split()[5]
    brightness_file.close()

    #Some code that isn't relative to this problem
    #The wanted birghtness is called now brightness_value

    #Setting the brightness
    os.system("powershell (Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,"+str(brightness_value)+")")

这在其他方面有效,但不是一个有效的方法。我是否可以将PowerShell导入一个线程,这样它就不再需要打开PowerShell了?你知道吗

我试过这样的方法 导入操作系统

#Import powershell
os.system("powershell")
#Get the brightness
os.system("Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness >brightness.txt")

但它只是在cmd中打开PowerShell,不继续代码。你知道吗


Tags: the方法txtgetvalueosrootwmi