pythontkinterbrightness

How to create a brightness toggle on Python Tkinter


I am creating a brightness toggle on Python. I am using the Tkinter module to create a sider function. I have successfully created a slider to increase/decrease the volume. However, I want to translate this into one where I can increase/decrease the screen's brightness. Here is my volume code and output.

I am currently using macOS Monterey Version 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()

Solution

  • Are you trying to achieve something like this? Or do you want to control desktop brightness?

    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()