如何在 Python Tkinter 上创建亮度切换

How to create a brightness toggle on Python Tkinter

我正在 Python 上创建亮度切换。我正在使用 Tkinter 模块创建辅助功能。我已经成功地创建了一个 increase/decrease 音量的滑块。但是,我想将其转换为 increase/decrease 屏幕亮度。这是我的音量代码和输出。

我目前使用的是 macOS Monterey 版本 12.2.1。

        #The GUI player
import pygame
from pygame import mixer
from tkinter import *


# Makes a new window and stores it inside the root variable.
root = Tk()

mixer.init() # initialising the mixer

# Coding the background music
def play_music():
    pygame.mixer.music.load("ArcadeMusic copy.mp3")
    pygame.mixer.music.play(-1)

def set_vol(val):
    volume = int(val) / 100
    mixer.music.set_volume(volume)

root.geometry('300x70')
root.title("Volume")

text = Label(root, text = "Drag the slider to adjust volume")
text.pack()

scale = Scale(root,from_=0,to=100 , orient=HORIZONTAL, command=set_vol)
scale.pack()

play_music()
root.mainloop()

你想实现这样的目标吗?或者你想控制桌面亮度?

import tkinter

def find( obj ):
    name = f"{type(obj).__name__}:n    "
    try:
        return name + "n    ".join( [ f"{x} = '{obj.cget(x)}'" for x in obj.keys() ] )
    except:
        return f"'{obj}' has no keys attribute"

class brightness:

    def __init__( self ):
        self.master = tkinter.Tk()
        self.master.update_idletasks()
        self.color = 15790320 # #f0f0f0 = SystemButtonFace
        self.var = tkinter.IntVar( self.master, value = self.color )
        self.flexx( self.master )

        self.label = tkinter.LabelFrame(
            self.master, labelanchor = 'n', text = self.master['background'] )
        self.label.grid( row=0, column=0, columnspan=2, sticky='nsew' )
        self.flexx( self.label, r=None )
        self.flexx( self.label, r=None , c=1 )

        

        self.scroll = tkinter.Scale(
            self.label, orient = 'horizontal',
            resolution = 65793, label = 'Brightness Control',
            from_ = 0, to = 16777215, variable = self.var,
            command = self.control )
        self.scroll.grid( row = 1, column=0, columnspan=2, sticky='ew' )

        self.master.geometry( '200x200' )
        self.master.minsize( 334, 113 )

    def flexx( self, o, r = 0, c = 0, rw = 1, cw = 1 ):
        if r != None:
            o.rowconfigure( r, weight = rw )
        if c != None:
            o.columnconfigure( c, weight = cw )

    def convert( self ):
        col = '#' + ( '000000' + hex( self.color )[2:])[~5:]
        self.var.set( self.color )
        self.label['text'] = col
        self.master.tk_setPalette( col )

    def control_up( self ):
        self.color += 65793
        if self.color > 16777215:
            self.color = 15790320
        col = self.convert( )

    def control_down( self ):
        self.color -= 65793
        if self.color < 0: # 15790320:
            self.color = 16777215
        col = self.convert( )

    def control( self, n ):
        self.color = int( n )
        col = self.convert( )

if __name__ == '__main__':

    bright = brightness( )
    tkinter.mainloop()