'Adjust screen brightness without showing the brightness-adjusted popup using Python3

I am currently working on an app using Python that automatically adjusts the screen brightness in accordance with the contents on the screen (os: macOS Monterey). The only issue I am having is that whenever the brightness is adjusted, there is this usual popup on the screen. It is very distracting and greatly hampers the user experience.

The "brightness-adjusted" popup

Here's the code:

os.system('osascript -e \'tell application "System Events"\' -e \'key code 145\' -e \' end tell\'') # decrease brightness

os.system('osascript -e \'tell application "System Events"\' -e \'key code 144\' -e \' end tell\'') # increase brightness

These lines basically execute AppleScript code on the terminal using the os.system method to press the brightness-down and brightness-up keys, respectively.

So my question is, is there any way I can adjust the brightness without showing the brightness-adjusted popup, such that when the users themselves adjust the brightness, the pop-up is shown, but not when the code does the same?

Edit #1 -

I tried changing the AppleScript part, as @Mark Setchell suggested

Here's the code:-

scpt = '''
    tell application "System Preferences"
        reveal anchor "displaysDisplayTab" of pane "com.apple.preference.displays"
    end tell
    tell application "System Events" to tell process "System Preferences" to tell window "Built-in Retina Display"
        set value of value indicator 1 of slider 1 of group 2 of tab group 1 to (get (value of value indicator 1 of slider 1 of group 2 of tab group 1) + 0.0625)
    end tell
    quit application "System Preferences"
    '''

p = Popen(['osascript', '-'], stdin=PIPE, stdout=PIPE, stderr=PIPE, universal_newlines=True)
stdout, stderr = p.communicate(scpt)

But this is giving this error -

execution error: System Events got an error: Can’t get tab group 1 of process "System Preferences". Invalid index.

I understand that this code was for an older version of macOS, but I can't seem to find the current alternative for macOS Monterey. Any help would be appreciated.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source