While loop and ttkbootstrap GUI

Hi, i decided to create simple program that changes your computer brightness based on time you inputed(in my first steps for testing it i’m working with seconds). I made it, and now i want to give it gui for future using, so i used ttkbootstrap from tkinter
The problem is i want program to check each second, but it works only without GUI, when the window of input is working, my while loop doesn’t work(so program can’t check each second). Do you know what’s the thing or can i achieve it in much better way? Here’s my code:

from ensurepip import bootstrap
from struct import pack
from turtle import left
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                                                 #LIBRARIES

working = False
firstSecond = int()
firstChange = int()
secondSecond = int()
secondChange = int()                                    #VARIABLES

root = tb.Window(themename="superhero")
root.title("TITLE")
root.geometry("800x500")

def program():                                             #THE WHOLE PROGRAM
    currentSecond = int(strftime("%S"))
    print(currentSecond)
    if currentSecond >= firstSecond and currentSecond < secondSecond:
        set_brightness(firstChange)
    elif currentSecond >= secondSecond or currentSecond < firstSecond:
        set_brightness(secondChange)

def save():            #IF USER PRESSES BUTTON, IT SAVES VARIABLES
    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 working = True
    except:
        print("LOL ERROR")

while working == True:
        program()
        sleep(1)
        
myLabel = tb.Label(text="Brightness Program", font=("Berlin Sans FB", 35), bootstyle="default")
myLabel.pack(pady=10)
                                                            #LABELS AND ENTRIES
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)

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)

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)

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)

myButton = tb.Button(text="SAVE", bootstyle="primary, outline", command=save)
myButton.pack(pady=30)

root.mainloop()

Hi !

Since root.mainloop() is a blocking call, you need a solution that integrates with your Tkinter app. You cannot have a completely separate layout like you tried to do.

A convenient way to do it is to use the root.after method. It registers a callback that will be called after a given delay, and in which you can perform your custom action. Tkinter handles the call, so it’s perfectly integrated in the app. See basic examples here: python - How do you run your own code alongside Tkinter's event loop? - Stack Overflow, and other more complex ones here: python - Tkinter: How to use threads to preventing main event loop from "freezing" - Stack Overflow

As a side note, you should avoid using import * statements, the global keyword, and would likely benefit from embedding your app inside a class.

1 Like

Thank you much! I feel it’ll help so i’ll try