Hi! Need feedback
How would you rate my brightness program made on python, using ttkbootstrap? I implemented day / night time that you write in hours, brightness value in percents(i called it force in the program), their sensitivity(how quick brightness changes), two app themes, speaker(text above all and below the app name, that shows a little bit information) and theme changer for windows only, if it’s night time. It also able to hide in the tray if you try to close it(to close or open from tray just right click)
Here all code! :
from concurrent.futures import thread
from ensurepip import bootstrap
import glob
from struct import pack
import threading
from turtle import left
from unittest import skip
from screen_brightness_control import get_brightness, set_brightness, fade_brightness
from time import sleep, strftime
from tkinter import *
from ttkbootstrap.constants import *
import ttkbootstrap as tb
import pystray
import PIL.Image
import threading
import ttkbootstrap.style
import winreg
firstSecond = int()
firstChange = int()
secondSecond = int()
secondChange = int()
daySmoothness = int()
eveningSmoothness = int()
brightnessUp = None
dayTime = None
x = get_brightness()
currentBrightness = int(x[0])
speakerText = ""
theme = 0
anim = 0
check = 0
root = tb.Window(themename="darkly")
root.title("BRIGHTNESS PROGRAM")
root.geometry("700x650")
root.iconbitmap("lightIcon.ico")
try:
savedData = open("savedData.txt", "r")
content = savedData.read()
savedData.close()
content = content.split()
speakerText = "Savedata file found"
except:
savedData = open("savedData.txt", "w")
content = savedData.write("")
savedData.close()
speakerText = "Created saveddata file"
savedData = open("savedData.txt", "r")
content = savedData.read()
savedData.close()
content = content.split()
try:
firstSecond = int(content[0])
firstChange = int(content[1])
secondSecond = int(content[2])
secondChange = int(content[3])
daySmoothness = int(content[4])
eveningSmoothness = int(content[5])
theme = int(content[6])
check = int(content[7])
if theme == 1:
ttkbootstrap.Style("morph")
else:
ttkbootstrap.Style("darkly")
sleep(1)
except:
speakerText = "There are no prepared variables"
sleep(1)
def ChangeBrightness():
global currentBrightness
global dayTime
global anim
global check
x = get_brightness()
currentBrightness = int(x[0])
if dayTime == True:
if currentBrightness > firstChange:
currentBrightness -= 1
if anim == 0:
anim = 1
else:
anim = 0
global speakerText
if anim == 0:
speaker.config(text="Brightness-")
else:
speaker.config(text="Brightness -")
set_brightness(currentBrightness)
elif currentBrightness < firstChange:
currentBrightness += 1
if anim == 0:
anim = 1
else:
anim = 0
if anim == 0:
speaker.config(text="Brightness+")
else:
speaker.config(text="Brightness +")
set_brightness(currentBrightness)
elif currentBrightness == firstChange:
sleep(1)
speaker.config(text="Brightness == First Change")
else:
if currentBrightness > secondChange:
currentBrightness -= 1
if anim == 0:
anim = 1
else:
anim = 0
if anim == 0:
speaker.config(text="Brightness-")
else:
speaker.config(text="Brightness -")
set_brightness(currentBrightness)
elif currentBrightness < secondChange:
currentBrightness += 1
if anim == 0:
anim = 1
else:
anim = 0
if anim == 0:
speaker.config(text="Brightness+")
else:
speaker.config(text="Brightness +")
set_brightness(currentBrightness)
elif currentBrightness == secondChange:
sleep(1)
speaker.config(text="Brightness == Second Change")
if dayTime == True:
threading.Timer(daySmoothness, ChangeBrightness).start()
elif dayTime == False:
threading.Timer(eveningSmoothness, ChangeBrightness).start()
else:
threading.Timer(10, ChangeBrightness).start()
def changeTheme():
global theme
if theme == 1:
ttkbootstrap.Style("darkly")
theme = 0
elif theme == 0:
ttkbootstrap.Style("morph")
theme = 1
def program():
currentSecond = int(strftime("%H"))
global dayTime
global check
if currentSecond >= firstSecond and currentSecond < secondSecond:
dayTime = True
if check == 1:
keypath = r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
valuename = "AppsUseLightTheme"
darkmodevalue = 1
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, keypath, 0, winreg.KEY_WRITE)
winreg.SetValueEx(key, valuename, 0, winreg.REG_DWORD, darkmodevalue)
winreg.CloseKey(key)
elif currentSecond >= secondSecond or currentSecond < firstSecond:
dayTime = False
if check == 1:
keypath = r"Software\Microsoft\Windows\CurrentVersion\Themes\Personalize"
valuename = "AppsUseLightTheme"
darkmodevalue = 0
key = winreg.OpenKey(winreg.HKEY_CURRENT_USER, keypath, 0, winreg.KEY_WRITE)
winreg.SetValueEx(key, valuename, 0, winreg.REG_DWORD, darkmodevalue)
winreg.CloseKey(key)
threading.Timer(60, program).start()
programT = threading.Thread(target=program)
programT.daemon = True
programT.start()
ChangeBrightnessT = threading.Thread(target=ChangeBrightness)
ChangeBrightnessT.daemon = True
ChangeBrightnessT.start()
def save():
try:
global firstSecond
firstSecond = int(fsEntry.get())
global secondSecond
secondSecond = int(ssEntry.get())
global firstChange
firstChange = int(ffEntry.get())
global secondChange
secondChange = int(sfEntry.get())
global daySmoothness
daySmoothness = int(smEntry.get())
global eveningSmoothness
eveningSmoothness = int(smEntry2.get())
global theme
global check
savedData = open("savedData.txt", "w")
savedData.write(f"{firstSecond}\n{firstChange}\n{secondSecond}\n{secondChange}\n{daySmoothness}\n{eveningSmoothness}\n{theme}\n{check}")
savedData.close()
global speakerText
speaker.config(text="Saved!")
except:
speaker.config(text="Wrong text")
def checker():
global check
if check == 0:
check = 1
else:
check = 0
def restoreFromTray(icon, item):
global trayed
trayed = False
root.deiconify()
icon.stop()
def close(icon, item):
icon.stop()
root.quit()
def minimizeToTray():
root.withdraw()
image = PIL.Image.open("lightIcon.ico")
menu = (pystray.MenuItem("Show", restoreFromTray), pystray.MenuItem("Quit", close))
icon = pystray.Icon("BRIGHTNESS PROGRAM", image, "BP", menu)
icon.run_detached()
root.protocol("WM_DELETE_WINDOW", minimizeToTray)
myLabel = tb.Label(text="Brightness Program", font=("Berlin Sans FB", 35), bootstyle="default")
myLabel.pack(pady=0)
speaker = tb.Label(text=f"{speakerText}", font=("Berlin Sans FB", 15), bootstyle="default")
speaker.pack(pady=0)
fsLabel = tb.Label(text="First time", font=("Berlin Sans FB", 10), bootstyle="default")
fsLabel.pack()
fsEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 10), foreground="light blue", width=4)
fsEntry.pack(pady=5)
fsEntry.insert(0, firstSecond)
ffLabel = tb.Label(text="First force", font=("Berlin Sans FB", 10), bootstyle="default")
ffLabel.pack()
ffEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 10), foreground="light blue", width=3)
ffEntry.pack(pady=5)
ffEntry.insert(0, firstChange)
smLabel = tb.Label(text="Day Smoothness", font=("Berlin Sans FB", 10), bootstyle="default")
smLabel.pack()
smEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 12), foreground="light blue", width=3)
smEntry.pack(pady=5)
smEntry.insert(0, daySmoothness)
myLabel2 = tb.Label(text="|", font=("Berlin Sans FB", 10), bootstyle="default")
myLabel2.pack(padx=5)
ssLabel = tb.Label(text="Second time", font=("Berlin Sans FB", 10), bootstyle="default")
ssLabel.pack()
ssEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 10), foreground="light blue", width=4)
ssEntry.pack(pady=5)
ssEntry.insert(0, secondSecond)
sfLabel = tb.Label(text="Second force", font=("Berlin Sans FB", 10), bootstyle="default")
sfLabel.pack()
sfEntry = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 10), foreground="light blue", width=3)
sfEntry.pack(pady=5)
sfEntry.insert(0, secondChange)
smLabel2 = tb.Label(text="Evening Smoothness", font=("Berlin Sans FB", 10), bootstyle="default")
smLabel2.pack()
smEntry2 = tb.Entry(root, bootstyle="info", font=("Berlin Sans FB", 12), foreground="light blue", width=3)
smEntry2.pack(pady=5)
smEntry2.insert(0, eveningSmoothness)
myButton = tb.Button(text="SAVE", bootstyle="primary, outline", command=save)
myButton.pack(pady=20)
themeButton = tb.Button(text="☀", bootstyle="primary, outline", command=changeTheme)
themeButton.pack(side=LEFT, padx=10)
if check == 0:
var1 = IntVar(value=0)
else:
var1 = IntVar(value=1)
myCheck = tb.Checkbutton(bootstyle="primary", text="Change Windows Theme in the Evening", variable=var1, onvalue=1, offvalue=0, command=checker)
myCheck.pack(pady=0, side=LEFT)
root.mainloop()
I’m relatively a beginner, so the whole program was made thankfully by using many guides, and of couse i see that you have to write down exact hours(without minutes) and i used a lot of lines(easily could make it slimmer) but it was planned for my inner use every of my day, and because i’m a beginner, i decided not to create too complex things, just make enough. What do you think? Maybe you have better ideas, similar experience or even better code suggest, i’ll be glad to read❗