我该如何使用Python在Windows中更改屏幕亮度,而不需要任何额外的Python包?
我还希望找到一种方法来获取当前屏幕亮度。
我认为需要使用ctypes,但我找不到任何有关如何实现的信息。
我很烦恼Windows上的屏幕亮度按钮会以非常大的增量调整屏幕亮度,因此我创建了一个Python tkinter程序,在任务栏上显示当前屏幕亮度,当我在tkinter窗口上滚动鼠标滚轮时,它会更改屏幕亮度:
我很烦恼Windows上的屏幕亮度按钮会以非常大的增量调整屏幕亮度,因此我创建了一个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"\r\nInstanceName : "))
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证书时遇到了问题。我搜索了一下并尝试修复它,但是我无法让它正常工作。