0

The code is as follows:

def TrainImages():
    assure_path_exists(os.path.join(SYSPATH,"TrainingImageLabel/"))
    recognizer = cv2.face_LBPHFaceRecognizer.create()
    faces, ID = getImagesAndLabels("TrainingImage")
    try:
        recognizer.train(faces, np.array(ID))
    except:
        mess.showwarning(title='No Registrations', message='Please Register someone first!!!')
        return
    recognizer.save(os.path.join(SYSPATH,"TrainingImageLabel\Trainner.yml"))
    res = "Profiles Saved and trained Successfully"
    instructionLabel.configure(text=res)
    promptLabel.configure(text='Total Registrations Done  : ' + str(max(ID)))
    time.sleep(3)
    displayUpdate()

In the above code, the instructionLabel.config and promptLabel.config are written first so they should at least be executed before sleep but instead, the code sleeps for 3 seconds and then all of the prompts show at once. I wanted to show the Total Registration Done bit with the Profile saved prompt for 3 seconds and then overwrite those prompts with detailed info.

Is sleep not ideal in the situation and maybe it is stopping the entire code then and there and not just the following lines from executing?

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • 1
    It is recommended you do not use `sleep` in a Tkinter app. Use `after` instead. This is because `sleep` blocks the ui thread and it can't update. `after` allows the ui thread to continue, then runs the command after the delay. Example: `root.after(3000, displayUpdate)`. – 001 May 06 '23 at 18:17
  • Beside the point, but [a bare `except` is bad practice](/q/54948548/4518341). Instead, use the specific exception you're expecting like `except ValueError`, or at least `except Exception`. – wjandrea May 06 '23 at 22:54
  • 1
    Pretty much NOTHING in Tkinter has an immediate visual effect. Only when you return to the mainloop, or explicitly call something like `.update_idletasks()`, are events processed and the display actually updated. – jasonharper May 06 '23 at 23:25

0 Answers0