如何使用python更改窗口中的屏幕亮度



如何使用python更改窗口中的屏幕亮度,而不使用任何额外的python包?我还想要一种方法来获得屏幕亮度目前是多少。我想这需要通过使用ctypes,但我找不到任何关于如何做到这一点的信息。

窗口上的屏幕亮度按钮以非常大的增量调整屏幕亮度的方式让我很恼火,所以我制作了一个python tkinter程序来在任务栏上显示当前屏幕亮度,当我在tkinter窗口上滚动鼠标轮时,它会更改屏幕亮度:

import subprocess
from tkinter import Canvas, Tk
win = Tk()
win.overrideredirect(True)
canvas = Canvas(win, bg = "#101010", highlightthickness = 0)
canvas.pack(fill = "both", expand = True)
def trim(string, l = None, r = None, include = False, flip = False):
string = str(string)
if l and l in string:
string = string[(string.rindex(l) if flip else string.index(l)) + (0 if include else len(l)):]
if r and r in string:
string = string[:(string.index(r) if flip else string.rindex(r)) + (len(r) if include else 0)]
return string
def set_screen_brightness():
subprocess.run(
["powershell",
f"(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,{current_brightness})"],
stdout = subprocess.PIPE, stderr = subprocess.STDOUT, shell = True)
def get_screen_brightness():
value = subprocess.check_output(
["powershell", f"Get-Ciminstance -Namespace root/WMI -ClassName WmiMonitorBrightness"],
shell = True)
return int(trim(value, l = "CurrentBrightness : ", r = r"rnInstanceName      : "))
current_brightness = get_screen_brightness()
brightness_text = canvas.create_text(30, 25, text = current_brightness,
font = ("Segue ui", 14, "bold"), fill = "White", anchor = "w")
sw, sh = win.winfo_screenwidth(), win.winfo_screenheight()
win.geometry(f"69x50+{sw - 505}+{sh - 49}")
def mouse_wheel_screen_brightness(scroll):
global current_brightness, mouse_wheel_screen_brightness_wa
if "mouse_wheel_screen_brightness_wa" in globals():
win.after_cancel(mouse_wheel_screen_brightness_wa)
current_brightness += 2 if scroll else -2
if current_brightness < 0: current_brightness = 0
if current_brightness > 100: current_brightness = 100
canvas.itemconfig(brightness_text, text = current_brightness)
mouse_wheel_screen_brightness_wa = win.after(100, lambda: set_screen_brightness())
win.bind("<MouseWheel>", lambda e: mouse_wheel_screen_brightness(e.delta > 0))
def topmost():
win.attributes("-topmost", True)
win.after(100, topmost)
topmost()
win.mainloop()

我相信一定有一种快速的方法可以改变ctypes中的屏幕亮度,而不需要任何额外的python包。通过使用对Powershell的子流程调用,屏幕亮度文本保持冻结。我想在不使用任何额外的python包的情况下进行安装,因为当我尝试安装python包时,它会说确认ssl证书时出现问题。我在谷歌上搜索并试图修复它,但我无法让它发挥作用。

在没有任何库的情况下,另一种方法是:

import subprocess

def run(cmd):
completed = subprocess.run(["powershell", "-Command", cmd], capture_output=True)
return completed
if __name__ == '__main__':
take_brightness = input("Please enter the brightness level: ")
command = "(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1," + take_brightness + ")"
hello_command = f"Write-Host {command}"
hello_info = run(hello_command)

使用pip安装屏幕亮度控制Windows:pip install screen-brightness-control

Mac/Linux:pip3 install screen-brightness-control我猜是

然后使用此代码

import screen_brightness_control as sbc
sbc.set_brightness(25) # Number will be any number between 0 and hundred
# Sets screen brightness to 25%

我在这里找到了屏幕亮度模块的信息

我使用了WMI库,它确实运行得很好。这是代码,但这是针对Windows的。我认为它是特定于操作系统的,所以如果它不适合你,你应该寻找更好的解决方案。

import wmi

brightness = 40 # percentage [0-100] For changing thee screen 
c = wmi.WMI(namespace='wmi')
methods = c.WmiMonitorBrightnessMethods()[0]    
methods.WmiSetBrightness(brightness, 0)

如果你喜欢,请投赞成票并接受答案。

PowerShell commands的帮助下,我们可以轻松地增加或降低窗口的亮度,而无需任何外部包装

WmiSetBrightness(1,<brightness % goes here>)

import subprocess
subprocess.run(["powershell", "(Get-WmiObject -Namespace root/WMI -Class WmiMonitorBrightnessMethods).WmiSetBrightness(1,40)"])

子流程是python附带的内部库。

只要运行上面的代码,希望这能奏效。

最新更新