Kartik Mehra
Kartik Mehra

Reputation: 107

tkinter - How to not process further code if error is not fixed - messagebox.showerror()

I have a tkinter app where I have Labels, Entries and Button. Whenever I click the button, the entries get passed to a function where they are verified according to a format.

For example -

Two fields - Employee Name, Employee Id

Now, I wish to check if username starts with "EMP" or not. For that I have made some functions(like checks if alphanumeric or not etc). If the username does not start with "EMP", what I am doing now is showing an error box like this

def tracking_images(self,emp_id,emp_name):
    emp_id, emp_name = emp_id.get(), emp_name.get()
    if len(emp_id) == 0:
        tk.messagebox.showerror("Field error", "Employee ID cannot be empty")
    elif len(emp_name) == 0:
        tk.messagebox.showerror("Field error", "Employee name cannot be empty")
    
    if not ValidationConditions().first_three_chars(emp_id):
        tk.messagebox.showerror("Field error", "Employee ID should start with EMP")
    ........
    ........
    #Some more code which I don't want user to have until he/she fixes the error from above checks.  <-------

Now, after the user clicks "Ok" to any prompt, The code which I don't want user to access still accesses.

How to not user process further until he fixes the errors from the above checks ?

Upvotes: 0

Views: 81

Answers (1)

r0w
r0w

Reputation: 165

You could approach this in a do-while manner (even if pyhton does not support this semantically)

Pseudo code would look like this:

while True:
    ask the name
    if the name passes the checks break out of the loop
    show errors

code to go to when the name is valid

EDIT: I forgot to note that, as mentioned below, this would have to be done in an extra thread.

Another thing that might work is putting the dialogs in a method that calls itself if the name is invalid to start over.

But I never tried that and cannot test it since I am commuting right now.

Upvotes: 1

Related Questions